YouTube2Text

[CS61C FA20] Lecture 04.2 - C Intro: Pointers, Arrays, Strings: Using Pointers Effectively — Transcript

by CS 61C Departmental · 2,059 words · 311 segments · language en · Watch on YouTube

Full transcript

  1. 0:00welcome back so have you scared your net
  2. 0:03have i scared you yet are you still
  3. 0:04looking forward to playing with pointers
  4. 0:05i know that there's a lot of things that
  5. 0:07are
  6. 0:07rough edged about them but they're
  7. 0:08really powerful and they're really fun
  8. 0:10let's actually get let's actually talk
  9. 0:11about how to use them effectively
  10. 0:14so remember before i mentioned that
  11. 0:16variables you declare them
  12. 0:18into x and now x is always going to be
  13. 0:20injured for the rest of the life of the
  14. 0:21program but pointers can actually move
  15. 0:22around a little bit
  16. 0:23if i say pointer uh int star p it means
  17. 0:26i'm going to point to
  18. 0:27any i can move it to point to this
  19. 0:29integer and then next line point it to
  20. 0:30that integer so that's actually easy to
  21. 0:32do and doesn't require any trouble
  22. 0:34you can in fact declare a pointer to a
  23. 0:37generic type which is
  24. 0:39called you clara you declared a void
  25. 0:41star pointer and that means you could
  26. 0:42actually
  27. 0:43with the more somewhere type casting
  28. 0:44which we'll talk about later i can then
  29. 0:46move it around now it's pointing to an
  30. 0:47end
  31. 0:48now i'm going to point it to a float now
  32. 0:49point to a care and you've got to really
  33. 0:51know what you're doing usually that's
  34. 0:52not what you want to do
  35. 0:53and so you probably typically not do
  36. 0:54that so that's kind of expert mode only
  37. 0:56but you can do that if you typecasted
  38. 0:58right you never have pointers to
  39. 1:00functions the picture the bottom right
  40. 1:02shows a pointer to a function that says
  41. 1:03i'm declaring this
  42. 1:05function pointer fn okay function
  43. 1:08pointer to be a pointer
  44. 1:10to a function that takes two void stars
  45. 1:12two generic pointers
  46. 1:14and returns an integer and foo is one of
  47. 1:17those guys
  48. 1:18okay foo satisfies that so now that
  49. 1:20means
  50. 1:21that for to call and that declares that
  51. 1:25actually
  52. 1:25makes the link to it okay address of foo
  53. 1:27so that pointer now fn in a way points
  54. 1:29to foo
  55. 1:30so if i actually want to call make use
  56. 1:34of that pointer i'd say star
  57. 1:35fn and then open print x and y
  58. 1:38so if it's that's the same as calling
  59. 1:40foo of x and y is star f n x and y in
  60. 1:43fact i'm going to show you example
  61. 1:44of doing this to try to write the higher
  62. 1:46order function map a little later that's
  63. 1:47pretty cool
  64. 1:49so we talked about structures before
  65. 1:51you've seen that before uh
  66. 1:52typed f a point this is like the hello
  67. 1:54world of abstract data types make a
  68. 1:56point
  69. 1:57i can make a point class make a point
  70. 1:58structure in c so it has two fields x
  71. 2:01and y
  72. 2:01i can get access to them and i make two
  73. 2:03here i'm making two points
  74. 2:05p one and p two each of those themselves
  75. 2:07have two fields bloop
  76. 2:08p1 and p2 p1 has x and y p2 has x and y
  77. 2:12point star p address says i'm going to
  78. 2:14make this p
  79. 2:15address be a pointer to a point it
  80. 2:17doesn't point there yet
  81. 2:18the next line says p address now equals
  82. 2:20address of p2 now
  83. 2:22p address points to p2 so now what can
  84. 2:24we do well
  85. 2:25the first line dot notation says i can
  86. 2:28int h
  87. 2:28equals p1 dot x that's fine that just
  88. 2:31grabs it before and sets it to h
  89. 2:32i can also set you know p one dot y
  90. 2:34equals p two dot y nothing special
  91. 2:36there i can also use the pointer and add
  92. 2:38an arrow notation so i can say p
  93. 2:40address arrow the field name so p
  94. 2:43address arrow x and then that just
  95. 2:46basically follows the pointer
  96. 2:47to whatever point it is happens to be p2
  97. 2:49but who knows
  98. 2:50and then what's that what's the x field
  99. 2:52grabs that and stuffs that into h
  100. 2:54you can also do something interesting
  101. 2:56which is you know that star
  102. 2:58p address is kind of like the point
  103. 2:59itself so if you have a parens around
  104. 3:01star p
  105. 3:02address you can then use dot notation
  106. 3:04now almost everybody 99.9
  107. 3:06of people if they have a pointer to a
  108. 3:08structure you use the arrow notation but
  109. 3:10you could also use the dot notation
  110. 3:12as well if you say p1 equals p2
  111. 3:16that says pointer p1 gets the values of
  112. 3:19p2 that's actually pretty interesting so
  113. 3:20that actually copies about it does not
  114. 3:22say sharing that says get the values of
  115. 3:24p1 and put them in now p1p2 have exactly
  116. 3:26the same x's and y's
  117. 3:27that's not a that's not a pointer that's
  118. 3:29not a sharing of the same storage that's
  119. 3:31two different storages
  120. 3:33each of now which have the same values
  121. 3:35that's really important that's different
  122. 3:36than a lot of other languages
  123. 3:37kind of cool you have a null pointer the
  124. 3:41pointer of
  125. 3:42all that point remember the null was a
  126. 3:44special thing and a pointer
  127. 3:46that's null means it points to nothing
  128. 3:48it's the only kind of save thing you can
  129. 3:49point
  130. 3:49you can declare it as okay how do you
  131. 3:51initialize something you have to have a
  132. 3:53kind of a sentinel value and null is the
  133. 3:55sentinel value of pointers
  134. 3:57null means zero which really means i can
  135. 3:59never have anything stored at zero
  136. 4:01remember this infinite array of memory
  137. 4:03well zero's reserved never have anything
  138. 4:05valuable there
  139. 4:06okay so that's important if you ever try
  140. 4:08to read or write a null pointer that's
  141. 4:09bad if you're trying to follow no point
  142. 4:10or get its value
  143. 4:11you can't do that so that's trouble so
  144. 4:13but it's a that's a way
  145. 4:15you can protect you say well it's p
  146. 4:16equal equal null then if it is i'm not
  147. 4:18going to touch it it means i didn't get
  148. 4:19some storage or
  149. 4:20it's null it's not it's not valid i
  150. 4:22can't use it to read or write any values
  151. 4:24you also remember that 0 is false so now
  152. 4:26i can actually say if p
  153. 4:28and q are pointers i can say you know if
  154. 4:30if not p
  155. 4:31meaning if not p that means only if
  156. 4:35if p was zero that's not true if p is um
  157. 4:38so if not p
  158. 4:39that's only true when p is zero i'll
  159. 4:41make sure i say this right so that's
  160. 4:42cool that says if not p that's a way to
  161. 4:44say you know p
  162. 4:44equal equals null is the same thing as
  163. 4:46saying if not p if i say if q
  164. 4:48that's true only if q is a good pointer
  165. 4:51so
  166. 4:51you can use the fact that pointers are
  167. 4:53zero when they're bad when they're point
  168. 4:55to null and
  169. 4:56non-zero when they're good point to some
  170. 4:57actual data as
  171. 4:59a useful test in your code
  172. 5:02you can point to variable things as i
  173. 5:05mentioned before you can have pointers
  174. 5:06to shorts or to integers or characters
  175. 5:09modern machines are bite addressable the
  176. 5:11olden days they were word addressable
  177. 5:12now they're by the address so i can get
  178. 5:14access to every byte okay that's kind of
  179. 5:15useful
  180. 5:16and a c pointer is just a way of
  181. 5:18thinking of an abstract abstracted
  182. 5:19memory address it's our way of
  183. 5:21interfacing with memory
  184. 5:23when you have type declarations the way
  185. 5:24of telling the c compiler this is how
  186. 5:26big these things are and this is how
  187. 5:28much data should be
  188. 5:29moving back and forth from two pieces
  189. 5:31but we actually want what's called
  190. 5:34word alignment and word alignment means
  191. 5:35that most machines are really fast if
  192. 5:38whatever the word size is we mentioned
  193. 5:40before word size is kind of
  194. 5:41the generic size of your
  195. 5:44address space so if i have a 32-bit
  196. 5:47machine that means my address space is
  197. 5:4832 bits i can access two to the 32
  198. 5:50different memory
  199. 5:51points and it also means that the data
  200. 5:54channels
  201. 5:55are almost always going to be 32 bits
  202. 5:56moving around it doesn't mean i can't
  203. 5:58move a 64-bit value just may not do it
  204. 5:59twice
  205. 6:00if my main channel if i made like 32
  206. 6:02it's like 32 lanes okay i can still send
  207. 6:0364 cars
  208. 6:04just have to do it twice and that's
  209. 6:06internal to you
  210. 6:08if you happen to have things be word
  211. 6:10aligned that means everything has to be
  212. 6:12aligned to whatever the size of your
  213. 6:13word is if a word and the machines we're
  214. 6:15going to use is
  215. 6:1632 bits that's a four bytes which means
  216. 6:19things have to be aligned on four byte
  217. 6:21values so that means you can sort a zero
  218. 6:24but you can't store anything
  219. 6:26let's say if it's i can store a
  220. 6:27character anywhere that's true
  221. 6:29but if it's a larger value like an
  222. 6:31integer or a float or a short or
  223. 6:33something you can't
  224. 6:34i'm sure it's aware because it might be
  225. 6:36half let's talk about ins for now so if
  226. 6:37i have an int
  227. 6:3932 machine i can't store the int like
  228. 6:41this i have to store the end exactly
  229. 6:43aligned with that
  230. 6:44so word alignment says you have to be
  231. 6:46aligned to 0
  232. 6:47which means the address of the of the
  233. 6:49end has to be 0
  234. 6:50or 4 or 8 or
  235. 6:53c if you look at the hex value the hex
  236. 6:55digit
  237. 6:57the nibble that's going to be 0 4
  238. 7:008 or c and then zero again so if i
  239. 7:03actually ask you what the address is if
  240. 7:05it's word line it's always gonna be zero
  241. 7:06four eight or c
  242. 7:07it's kind of a cool thing here's an
  243. 7:09example on the right of a nice picture
  244. 7:10that shows a short which in this case is
  245. 7:12going to be
  246. 7:1316 bits wide and there's a pointer to
  247. 7:15that short it happens to live there
  248. 7:18this is an integer uh this is x
  249. 7:20appointed to x and you see that it lives
  250. 7:22at 48.
  251. 7:23again that's a multiple of four so we're
  252. 7:24good there and there's a character that
  253. 7:26can live anywhere so that's kind of a
  254. 7:27nice thing okay
  255. 7:30sizeof is an operator you're going to
  256. 7:31see used a lot later when we talk about
  257. 7:33reserving memory from up from preserving
  258. 7:36space from your memory system
  259. 7:38and size of any type is going to tell
  260. 7:39you how many bytes wide that type is
  261. 7:42so size of car is one by the definition
  262. 7:45of c999
  263. 7:46but remember you don't know how big size
  264. 7:47of events are so you actually can go to
  265. 7:49a new machine a great program to write
  266. 7:50on your first machine you're just
  267. 7:52you know encountering someone drops a
  268. 7:53new machine i just invented a new
  269. 7:54machine
  270. 7:55first thing you write is printf
  271. 7:56sizeofint how big are the ins there
  272. 7:58how big are the doubles how big are the
  273. 7:59longs that little table i showed you
  274. 8:01before
  275. 8:02had like long long long inch shorts do a
  276. 8:04whole table that says
  277. 8:05and this machine shorts are printf
  278. 8:09percent d size of short it's our
  279. 8:12print percent d size of ins and you keep
  280. 8:16doing this so actually be kind of neat
  281. 8:17to play with that as a
  282. 8:19as you as you move that as you ported we
  283. 8:20learned the word porting that code to
  284. 8:22different machines be kind of fun to see
  285. 8:23what the output of that was
  286. 8:25this is why in types exist so you have
  287. 8:27to kind of guess what the size of those
  288. 8:28guys are but sizeof is really useful to
  289. 8:30find out how big they are
  290. 8:31you can also pass in size of a struct
  291. 8:33type and a struct type says oh i make
  292. 8:35this struct that has an integer and it
  293. 8:37has a character
  294. 8:38that's four standard machine and this is
  295. 8:41five
  296. 8:41if i say sizeobstruct is going to tell
  297. 8:43me five or tell me eight
  298. 8:45does it actually make sure the struct is
  299. 8:47word aligned
  300. 8:48so does that mean even if i only use a
  301. 8:50character does it actually get a use up
  302. 8:51that whole int even though i only use
  303. 8:53one of those four bytes it's going to
  304. 8:54use a ball for because it has to be word
  305. 8:56aligned so
  306. 8:56you can ask size of an int plus a
  307. 9:00character to see whether it's
  308. 9:01they actually make sure you word align
  309. 9:02all of your structs as well okay
  310. 9:04we'll see size of a little bit later and
  311. 9:06we'll learn more in the next video

About this transcript

This page contains the full transcript of [CS61C FA20] Lecture 04.2 - C Intro: Pointers, Arrays, Strings: Using Pointers Effectively by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 2,059 words across 311 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.