YouTube2Text

[CS61C FA20] Lecture 04.3 - C Intro: Pointers, Arrays, Strings: Arrays — Transcript

by CS 61C Departmental · 2,797 words · 440 segments · language en · Watch on YouTube

Full transcript

  1. 0:00and welcome back let's now talk about
  2. 0:03c arrays so arrays
  3. 0:07are similar to what you've probably seen
  4. 0:09in java you
  5. 0:10declare them by saying what type it is
  6. 0:12and how many you want
  7. 0:14so here the first line says int ar of 2
  8. 0:16says
  9. 0:17i want two integers side to side the
  10. 0:20most important thing you understand
  11. 0:21about arrays is that
  12. 0:22it's a way of asking c for a contiguous
  13. 0:25block of memory
  14. 0:26i need to have two integers side by side
  15. 0:28you can't have them here with a gap in
  16. 0:30the middle you have them together
  17. 0:32you can also declare and initialize them
  18. 0:34at the same time without saying the
  19. 0:35space so
  20. 0:36ar open square back open close square
  21. 0:39bracket equals
  22. 0:40two numbers um initializes those two
  23. 0:43spots and says there's gonna be two of
  24. 0:44them
  25. 0:44and the way you access them is the
  26. 0:46square bracket notation you've seen from
  27. 0:47python
  28. 0:48and from java ar of num or num is some
  29. 0:50value
  30. 0:51and it's zero index it's really
  31. 0:52important
  32. 0:54so mostly arrays and pointers are
  33. 0:56identical actually
  34. 0:58they're mostly the same uh so if i say
  35. 0:59care star string
  36. 1:01or care string open square bracket the
  37. 1:03first says the string
  38. 1:05is a pointer to a character array to a
  39. 1:08i even say character array um and the
  40. 1:10second says that
  41. 1:12string is an array so first a string is
  42. 1:14a pointer
  43. 1:15to a contiguous block of characters
  44. 1:19and the second is that string is a
  45. 1:20character array the neural identical
  46. 1:23people often almost the same almost
  47. 1:25always even confuse them in the way they
  48. 1:26describe them
  49. 1:28what's different is that you cannot
  50. 1:29increment uh
  51. 1:31an array the open square bracket you
  52. 1:32can't increment it you can't move the
  53. 1:34pointer you can for the pointer but you
  54. 1:35can't this so if you have a big array
  55. 1:37and you're
  56. 1:37moving across you can't increment it if
  57. 1:39you have open square brackets
  58. 1:40that's really the only it's very subtle
  59. 1:42the way you declare declare a filled
  60. 1:44array you'll
  61. 1:44only do it with the guy on the right not
  62. 1:46the guy on the left but you can think of
  63. 1:48it
  64. 1:48as an array variable points to the first
  65. 1:50element i mean there's they're also
  66. 1:51subtle some other ways of where they're
  67. 1:53stored and i'll talk about that in a
  68. 1:54second but
  69. 1:55that's the big idea okay so mostly
  70. 1:56they're the same i'll say they're the
  71. 1:57same
  72. 1:59what that means is if you have ar and
  73. 2:01it's an array variable
  74. 2:04but it looks like a pointer in most
  75. 2:05respects so you can say ar of 0
  76. 2:07you can also say star ar remember ar was
  77. 2:09declared as an array
  78. 2:11you can still say star ar you can also
  79. 2:13say ar of 0.
  80. 2:15you can say ar of 2 you can also say
  81. 2:17star
  82. 2:18ar plus 2. this is the first time i've
  83. 2:20shown you pointer arithmetic and the
  84. 2:22plus two says
  85. 2:23look whatever ar is pointing to i don't
  86. 2:25want that one the zeroth element
  87. 2:27i don't want the next one that's the
  88. 2:29first element well it's not the first
  89. 2:30it's the one element
  90. 2:31index i want the two index elements one
  91. 2:34two over
  92. 2:35so you say well dan how does it know how
  93. 2:36wide they are how how much to increment
  94. 2:38the pointer
  95. 2:39to increment that um so incrementing the
  96. 2:42point
  97. 2:42how how far to go across to get that
  98. 2:44value uh
  99. 2:45that's what types are about that's why
  100. 2:47you told it what type that
  101. 2:48was that pointer was two that array was
  102. 2:50for um
  103. 2:53they're only the declared space of an
  104. 2:55array is only valid they're temporary
  105. 2:57while the scope of that procedure is
  106. 2:59valid so here's
  107. 3:00care starfu and make a new character and
  108. 3:02make a new subroutine called foo
  109. 3:04pretty cool i'm going to return i'm
  110. 3:06going to return uh
  111. 3:07a pointer and then i say character of
  112. 3:10string of 32 i'm making now some local
  113. 3:12storage
  114. 3:13uh array of 32 32 characters calling it
  115. 3:17string
  116. 3:18and i'm going to return that you're not
  117. 3:20supposed to do that because
  118. 3:21that space was only available in that
  119. 3:24subroutine
  120. 3:25so only what foo was live there was that
  121. 3:27a string available and the moment foo
  122. 3:29ends
  123. 3:29the system might reuse that so you don't
  124. 3:31have access that's a
  125. 3:32key error we're going to mention that
  126. 3:34many more times but don't
  127. 3:35do that don't do what we did here in foo
  128. 3:37you don't have access to that once the
  129. 3:39procedure subroutine returns
  130. 3:43i mentioned before the zero index so if
  131. 3:45the race size is n you want to access
  132. 3:46zero to n minus one that's your range of
  133. 3:48things you can get to
  134. 3:49the problem is this particular slide is
  135. 3:53talking about
  136. 3:55a style thing this is really a style
  137. 3:56thing here so this the top line of code
  138. 3:59looks right like doesn't this look right
  139. 4:00look at it
  140. 4:01into i are of 10 for i
  141. 4:04is 0 less than 10 i plus plus looks good
  142. 4:06right
  143. 4:08well the problem is you've written 10
  144. 4:10you've kind of hard coded 10
  145. 4:12twice and what we want to teach you with
  146. 4:15621c is not just
  147. 4:16how c works but actually good style and
  148. 4:18so
  149. 4:19one of the things we want to make sure
  150. 4:21inculcate in this class
  151. 4:23is that you understand that the single
  152. 4:25source of truth is a valuable thing
  153. 4:27that's the reason why google drive and
  154. 4:29google
  155. 4:30documents are so great you have a single
  156. 4:31source of truth and 25 people are doing
  157. 4:33it rather than passing out
  158. 4:34version final final final no version and
  159. 4:37oh version 17 right
  160. 4:38crazy we look back and we ask ourselves
  161. 4:40why do we ever do that
  162. 4:42same thing here and see once you realize
  163. 4:44that you're going to have 10 hard-coded
  164. 4:46you should
  165. 4:46the hair in the back of your neck should
  166. 4:48go up i should say you know what i
  167. 4:49shouldn't do this how do i do this
  168. 4:50better well one way to do is you say int
  169. 4:52array size is 10
  170. 4:53before and ansi days before cnn you have
  171. 4:56to have pound signed
  172. 4:57array size 10. um pound size pound sign
  173. 5:00define
  174. 5:01array size 10. you have to def global
  175. 5:03define this as a as
  176. 5:04a pound defined in a c preprocessor
  177. 5:06directive
  178. 5:08here int array size equals 10. so into i
  179. 5:11a of array size this is a dynamic array
  180. 5:13again this came from c9
  181. 5:15and now i can say for i equals zero i'd
  182. 5:17list an array size
  183. 5:18now i only have one ten if i want to now
  184. 5:20change it
  185. 5:21i want to make a 20. now i change it one
  186. 5:23place so look at this and
  187. 5:25you know look well young padawan as you
  188. 5:27understand the
  189. 5:28the ways that that the pro tip for how
  190. 5:31to do this you want to really have any
  191. 5:33hard-coded value have it there once
  192. 5:35abstraction okay so use indirection
  193. 5:37indirection can save you a lot of
  194. 5:38trouble
  195. 5:39and i mean don't have to maintain two
  196. 5:40copies of 10.
  197. 5:43here's the other thing arrays don't know
  198. 5:46when their bounds are being exceeded
  199. 5:47this is c
  200. 5:48if there's ever a question of whether c
  201. 5:49checks or something the answer is always
  202. 5:51no
  203. 5:51c doesn't do any of those checks c says
  204. 5:53i'm just gonna let you run as fast as
  205. 5:54you want because basically i'm gonna
  206. 5:56compile it down to raw machine code i'm
  207. 5:57not gonna do those checks for you those
  208. 5:59sanity checks or make sure you're
  209. 6:00between zero and n minus one
  210. 6:02no doesn't know that the other downside
  211. 6:04is
  212. 6:05an array doesn't carry with it it's its
  213. 6:07size
  214. 6:08so that's a little bit of a bummer so if
  215. 6:09you ever pass an array around
  216. 6:11you have to pass the array and you got
  217. 6:13to pass the size of the array
  218. 6:15um or at least the number that you're
  219. 6:16going to be searching up to you can pass
  220. 6:18it you know a rate of size 100 if you
  221. 6:19only pass
  222. 6:2010 and then only think it's actually
  223. 6:21only 10 long and it won't go past that
  224. 6:23but you you need to kind of pass these
  225. 6:24two numbers along um there's only one
  226. 6:27small
  227. 6:28um asterisk to it exception which is
  228. 6:30strings
  229. 6:31because we've all agreed that zero
  230. 6:33character is a special way to terminate
  231. 6:34a string
  232. 6:35so if i'm passing a string to you you
  233. 6:37can keep reading until you get to zero
  234. 6:39you don't have to pass the string and
  235. 6:40the number now you can also write the
  236. 6:41number and then you
  237. 6:42you know you'll stop short of that but
  238. 6:44strings you don't have to pass because
  239. 6:45we've all agreed that zero
  240. 6:46is a terminate a null terminator at the
  241. 6:48end of a string but every other array
  242. 6:51every other array you have to pass in
  243. 6:52the array and the size or at least the
  244. 6:54size
  245. 6:55up two you want to be checking okay
  246. 6:57you're going to see some errors
  247. 6:58as you're coding in c you wouldn't be
  248. 7:01learning if you weren't finding irish
  249. 7:02you know you know if you're
  250. 7:03you're not learning how to ride a
  251. 7:04bicycle if you don't scrape your knee
  252. 7:05once or twice you've got to kind of
  253. 7:06try whatever if i wiggle the wheels what
  254. 7:08if i turn really steep well you hurt
  255. 7:10yourself and you're following but if i
  256. 7:11pedal when i'm making it
  257. 7:12i did this pedaling while i'm making a
  258. 7:14turn all of a sudden that pedal hits the
  259. 7:16drawer yeah
  260. 7:17so you got to try all these things to
  261. 7:18know what the bounds and range is so
  262. 7:19that when you get to a dangerous
  263. 7:20situation you won't know to do that
  264. 7:21you won't do that so
  265. 7:25segmentation faults and bus arrows are
  266. 7:26two kinds of errors that you'll see
  267. 7:28the idea of i'll just give you a little
  268. 7:30quickie um a segmentation fault says
  269. 7:33you're reading and writing to memory you
  270. 7:34don't have access to
  271. 7:35you're reading outside the segment the
  272. 7:37segment kind of is the way that your
  273. 7:38your access space a bus error often
  274. 7:41means your alignment is wrong
  275. 7:43so you know you want to talk about word
  276. 7:45alignment a couple of videos ago
  277. 7:46i'm going to read an integer if your
  278. 7:48integer pointer
  279. 7:50doesn't have if you say what's the
  280. 7:51address of that of that of that integer
  281. 7:53if that integer isn't the address if you
  282. 7:56look at it in hex isn't 0
  283. 7:574 8 or c you're not word aligned that's
  284. 8:00in a 32-bit machine so then if you read
  285. 8:03an integer i'm reading an integer but
  286. 8:04it's but the actual pointer is
  287. 8:055 well the actual you know the actual
  288. 8:07address of that integer is 5 well that's
  289. 8:10not that's not 4 eight you know 12 or
  290. 8:12not a multiple of four so that would be
  291. 8:14a bus there
  292. 8:15okay if it's if it's in the space over
  293. 8:16there you could have it could be
  294. 8:18ambiguous where i read it off alignment
  295. 8:19but i'm also
  296. 8:20off the area i'm supposed to be reading
  297. 8:21you can have one of the two one of the
  298. 8:23two so
  299. 8:23those are two ways if you see those
  300. 8:25arrows that's what they mean
  301. 8:26segmentation fault means i'm outside of
  302. 8:28memory i have control the buzzer and
  303. 8:29he's probably not aligned
  304. 8:32i mentioned before point arithmetic
  305. 8:34which is um if you have a pointer to
  306. 8:36something and you say plus a number i
  307. 8:37mentioned star a r plus two goes to the
  308. 8:39second
  309. 8:40uh element in that array however big
  310. 8:43they are it just moves over just
  311. 8:44strides however big they are that's what
  312. 8:46size was four size of was four
  313. 8:49so pointer plus n says well if it's a
  314. 8:51pointer to something
  315. 8:53um it adds here's the thing
  316. 8:56size of character is one so it's n times
  317. 8:59the size of the character so it says i'm
  318. 9:00going to move
  319. 9:01that many bytes across to the next guy
  320. 9:03so if it's an integer four
  321. 9:04if i say pointer plus one a pointer is a
  322. 9:06thousand say point is a nice multiple of
  323. 9:07four
  324. 9:08point thousand point plus one uh this
  325. 9:11means now the pointer is going to be if
  326. 9:12i ask address it's a thousand and four
  327. 9:14order plus one again it'll be a thousand
  328. 9:15and eight so it's moving by the size of
  329. 9:17that element
  330. 9:18so that's why we have to use that's why
  331. 9:19those types are useful to know how
  332. 9:21big to move when you say pointer plus or
  333. 9:23minus point or minus means go backwards
  334. 9:25go back in the array so put a plus or
  335. 9:26point of minus does that kind of be
  336. 9:28useful thing
  337. 9:29so you saw this review we saw this a
  338. 9:31couple uh lecture maybe last lecture
  339. 9:33the way you if i have a subroutine add
  340. 9:36one and i have y
  341. 9:37declared outside that scope and i pass
  342. 9:39in y y is still
  343. 9:40y is three initially add one is not
  344. 9:42going to affect y it's still going to be
  345. 9:44three after the call to add one
  346. 9:46the way you fix it is you pass in the
  347. 9:48address of y
  348. 9:49and now that pointer can manipulate y
  349. 9:51and when it's all done y is now four
  350. 9:53that's all review
  351. 9:55what how about what if i want to change
  352. 9:56a pointer so let's look at this code
  353. 9:58here let me pull this out
  354. 9:59and see if i got this here okay
  355. 10:02so i've got increment pointer increment
  356. 10:05pointer here all right what color my
  357. 10:07drawing here
  358. 10:08increment pointer son of a gun
  359. 10:14there okay
  360. 10:19i have increment pointer
  361. 10:23what does it do well let's look at it
  362. 10:25let's start here
  363. 10:26it's supposed to move the pointer across
  364. 10:28so here's a of 3
  365. 10:30okay make a nice filled array by the way
  366. 10:32you can also i didn't mention
  367. 10:33before you can fill the array and also
  368. 10:35tell it how big it is
  369. 10:37as long as they're consistent nothing
  370. 10:39critical so 50 60 70 in this nice array
  371. 10:41i have a pointer q we can think of q and
  372. 10:44a both point to the beginning of that
  373. 10:45array so here's my picture over here
  374. 10:47indicating that then i called increment
  375. 10:49q increment pointer of q
  376. 10:51i really want q to move over so i want q
  377. 10:54to be here
  378. 10:54and point to there that's what what i
  379. 10:56want is for this
  380. 10:58there to be my q okay so let's now call
  381. 11:00this well what would it do
  382. 11:02in star p i pass in the pointer to p now
  383. 11:04p and q
  384. 11:05are the same thing and i say p equals p
  385. 11:07plus one and i hope that p
  386. 11:09that q increments okay well
  387. 11:13what do you think happens let me try
  388. 11:14this erase this then what happens
  389. 11:1750 doesn't move at all why
  390. 11:21same reason as the slide before you
  391. 11:23can't change
  392. 11:25q without passing its that's all on
  393. 11:28three one two three
  394. 11:29address so you pass in queue's
  395. 11:32address well now what is the type for
  396. 11:36the increment pointer that's receiving
  397. 11:37that
  398. 11:38it's not a pointer to a int it's a
  399. 11:41pointer
  400. 11:42to a pointer to an int actually h and by
  401. 11:45the way we call it h as we call this
  402. 11:47a handle so it's a pointer to a pointer
  403. 11:49h is a handle
  404. 11:51so now it gets pretty let's try it so
  405. 11:54now
  406. 11:54q i pass in the address of q now i can
  407. 11:58change q
  408. 11:59star star h the way you get to that the
  409. 12:01way you dereference that handle
  410. 12:02is you say star h this is going to move
  411. 12:06where
  412. 12:06q is one over so let's try this
  413. 12:10i run this q moves one over and i print
  414. 12:13f star q so this is the same
  415. 12:15this didn't change down here the printf
  416. 12:16didn't change on there it's just that
  417. 12:18key thing was different
  418. 12:20and that key different was different i
  419. 12:22also changed this as well
  420. 12:23okay so now same idea and you're gonna
  421. 12:26say well dan look
  422. 12:27if i want what if i have a pointer to a
  423. 12:28pointer to a point the answer is
  424. 12:30yes i'm gonna short circuit you and say
  425. 12:32you can have a pointed to a pointer to a
  426. 12:34pointer
  427. 12:34three stars pointed to a pointer to a
  428. 12:36pointer to a pointer four stars
  429. 12:38you typically don't have more than two
  430. 12:39stars in the code and if you have three
  431. 12:42either you know what you're doing it's
  432. 12:43really complicated code or
  433. 12:45you probably don't need to have it so
  434. 12:46think about trying to remove that if you
  435. 12:48can but
  436. 12:49it gets more complicated once you have
  437. 12:50three layers but people have handles all
  438. 12:52the time
  439. 12:53which is a reasonable thing to do we'll
  440. 12:55see in the next video

About this transcript

This page contains the full transcript of [CS61C FA20] Lecture 04.3 - C Intro: Pointers, Arrays, Strings: Arrays by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 2,797 words across 440 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.