[CS61C FA20] Lecture 04.2 - C Intro: Pointers, Arrays, Strings: Using Pointers Effectively — Transcript
Full transcript
- 0:00welcome back so have you scared your net
- 0:03have i scared you yet are you still
- 0:04looking forward to playing with pointers
- 0:05i know that there's a lot of things that
- 0:07are
- 0:07rough edged about them but they're
- 0:08really powerful and they're really fun
- 0:10let's actually get let's actually talk
- 0:11about how to use them effectively
- 0:14so remember before i mentioned that
- 0:16variables you declare them
- 0:18into x and now x is always going to be
- 0:20injured for the rest of the life of the
- 0:21program but pointers can actually move
- 0:22around a little bit
- 0:23if i say pointer uh int star p it means
- 0:26i'm going to point to
- 0:27any i can move it to point to this
- 0:29integer and then next line point it to
- 0:30that integer so that's actually easy to
- 0:32do and doesn't require any trouble
- 0:34you can in fact declare a pointer to a
- 0:37generic type which is
- 0:39called you clara you declared a void
- 0:41star pointer and that means you could
- 0:42actually
- 0:43with the more somewhere type casting
- 0:44which we'll talk about later i can then
- 0:46move it around now it's pointing to an
- 0:47end
- 0:48now i'm going to point it to a float now
- 0:49point to a care and you've got to really
- 0:51know what you're doing usually that's
- 0:52not what you want to do
- 0:53and so you probably typically not do
- 0:54that so that's kind of expert mode only
- 0:56but you can do that if you typecasted
- 0:58right you never have pointers to
- 1:00functions the picture the bottom right
- 1:02shows a pointer to a function that says
- 1:03i'm declaring this
- 1:05function pointer fn okay function
- 1:08pointer to be a pointer
- 1:10to a function that takes two void stars
- 1:12two generic pointers
- 1:14and returns an integer and foo is one of
- 1:17those guys
- 1:18okay foo satisfies that so now that
- 1:20means
- 1:21that for to call and that declares that
- 1:25actually
- 1:25makes the link to it okay address of foo
- 1:27so that pointer now fn in a way points
- 1:29to foo
- 1:30so if i actually want to call make use
- 1:34of that pointer i'd say star
- 1:35fn and then open print x and y
- 1:38so if it's that's the same as calling
- 1:40foo of x and y is star f n x and y in
- 1:43fact i'm going to show you example
- 1:44of doing this to try to write the higher
- 1:46order function map a little later that's
- 1:47pretty cool
- 1:49so we talked about structures before
- 1:51you've seen that before uh
- 1:52typed f a point this is like the hello
- 1:54world of abstract data types make a
- 1:56point
- 1:57i can make a point class make a point
- 1:58structure in c so it has two fields x
- 2:01and y
- 2:01i can get access to them and i make two
- 2:03here i'm making two points
- 2:05p one and p two each of those themselves
- 2:07have two fields bloop
- 2:08p1 and p2 p1 has x and y p2 has x and y
- 2:12point star p address says i'm going to
- 2:14make this p
- 2:15address be a pointer to a point it
- 2:17doesn't point there yet
- 2:18the next line says p address now equals
- 2:20address of p2 now
- 2:22p address points to p2 so now what can
- 2:24we do well
- 2:25the first line dot notation says i can
- 2:28int h
- 2:28equals p1 dot x that's fine that just
- 2:31grabs it before and sets it to h
- 2:32i can also set you know p one dot y
- 2:34equals p two dot y nothing special
- 2:36there i can also use the pointer and add
- 2:38an arrow notation so i can say p
- 2:40address arrow the field name so p
- 2:43address arrow x and then that just
- 2:46basically follows the pointer
- 2:47to whatever point it is happens to be p2
- 2:49but who knows
- 2:50and then what's that what's the x field
- 2:52grabs that and stuffs that into h
- 2:54you can also do something interesting
- 2:56which is you know that star
- 2:58p address is kind of like the point
- 2:59itself so if you have a parens around
- 3:01star p
- 3:02address you can then use dot notation
- 3:04now almost everybody 99.9
- 3:06of people if they have a pointer to a
- 3:08structure you use the arrow notation but
- 3:10you could also use the dot notation
- 3:12as well if you say p1 equals p2
- 3:16that says pointer p1 gets the values of
- 3:19p2 that's actually pretty interesting so
- 3:20that actually copies about it does not
- 3:22say sharing that says get the values of
- 3:24p1 and put them in now p1p2 have exactly
- 3:26the same x's and y's
- 3:27that's not a that's not a pointer that's
- 3:29not a sharing of the same storage that's
- 3:31two different storages
- 3:33each of now which have the same values
- 3:35that's really important that's different
- 3:36than a lot of other languages
- 3:37kind of cool you have a null pointer the
- 3:41pointer of
- 3:42all that point remember the null was a
- 3:44special thing and a pointer
- 3:46that's null means it points to nothing
- 3:48it's the only kind of save thing you can
- 3:49point
- 3:49you can declare it as okay how do you
- 3:51initialize something you have to have a
- 3:53kind of a sentinel value and null is the
- 3:55sentinel value of pointers
- 3:57null means zero which really means i can
- 3:59never have anything stored at zero
- 4:01remember this infinite array of memory
- 4:03well zero's reserved never have anything
- 4:05valuable there
- 4:06okay so that's important if you ever try
- 4:08to read or write a null pointer that's
- 4:09bad if you're trying to follow no point
- 4:10or get its value
- 4:11you can't do that so that's trouble so
- 4:13but it's a that's a way
- 4:15you can protect you say well it's p
- 4:16equal equal null then if it is i'm not
- 4:18going to touch it it means i didn't get
- 4:19some storage or
- 4:20it's null it's not it's not valid i
- 4:22can't use it to read or write any values
- 4:24you also remember that 0 is false so now
- 4:26i can actually say if p
- 4:28and q are pointers i can say you know if
- 4:30if not p
- 4:31meaning if not p that means only if
- 4:35if p was zero that's not true if p is um
- 4:38so if not p
- 4:39that's only true when p is zero i'll
- 4:41make sure i say this right so that's
- 4:42cool that says if not p that's a way to
- 4:44say you know p
- 4:44equal equals null is the same thing as
- 4:46saying if not p if i say if q
- 4:48that's true only if q is a good pointer
- 4:51so
- 4:51you can use the fact that pointers are
- 4:53zero when they're bad when they're point
- 4:55to null and
- 4:56non-zero when they're good point to some
- 4:57actual data as
- 4:59a useful test in your code
- 5:02you can point to variable things as i
- 5:05mentioned before you can have pointers
- 5:06to shorts or to integers or characters
- 5:09modern machines are bite addressable the
- 5:11olden days they were word addressable
- 5:12now they're by the address so i can get
- 5:14access to every byte okay that's kind of
- 5:15useful
- 5:16and a c pointer is just a way of
- 5:18thinking of an abstract abstracted
- 5:19memory address it's our way of
- 5:21interfacing with memory
- 5:23when you have type declarations the way
- 5:24of telling the c compiler this is how
- 5:26big these things are and this is how
- 5:28much data should be
- 5:29moving back and forth from two pieces
- 5:31but we actually want what's called
- 5:34word alignment and word alignment means
- 5:35that most machines are really fast if
- 5:38whatever the word size is we mentioned
- 5:40before word size is kind of
- 5:41the generic size of your
- 5:44address space so if i have a 32-bit
- 5:47machine that means my address space is
- 5:4832 bits i can access two to the 32
- 5:50different memory
- 5:51points and it also means that the data
- 5:54channels
- 5:55are almost always going to be 32 bits
- 5:56moving around it doesn't mean i can't
- 5:58move a 64-bit value just may not do it
- 5:59twice
- 6:00if my main channel if i made like 32
- 6:02it's like 32 lanes okay i can still send
- 6:0364 cars
- 6:04just have to do it twice and that's
- 6:06internal to you
- 6:08if you happen to have things be word
- 6:10aligned that means everything has to be
- 6:12aligned to whatever the size of your
- 6:13word is if a word and the machines we're
- 6:15going to use is
- 6:1632 bits that's a four bytes which means
- 6:19things have to be aligned on four byte
- 6:21values so that means you can sort a zero
- 6:24but you can't store anything
- 6:26let's say if it's i can store a
- 6:27character anywhere that's true
- 6:29but if it's a larger value like an
- 6:31integer or a float or a short or
- 6:33something you can't
- 6:34i'm sure it's aware because it might be
- 6:36half let's talk about ins for now so if
- 6:37i have an int
- 6:3932 machine i can't store the int like
- 6:41this i have to store the end exactly
- 6:43aligned with that
- 6:44so word alignment says you have to be
- 6:46aligned to 0
- 6:47which means the address of the of the
- 6:49end has to be 0
- 6:50or 4 or 8 or
- 6:53c if you look at the hex value the hex
- 6:55digit
- 6:57the nibble that's going to be 0 4
- 7:008 or c and then zero again so if i
- 7:03actually ask you what the address is if
- 7:05it's word line it's always gonna be zero
- 7:06four eight or c
- 7:07it's kind of a cool thing here's an
- 7:09example on the right of a nice picture
- 7:10that shows a short which in this case is
- 7:12going to be
- 7:1316 bits wide and there's a pointer to
- 7:15that short it happens to live there
- 7:18this is an integer uh this is x
- 7:20appointed to x and you see that it lives
- 7:22at 48.
- 7:23again that's a multiple of four so we're
- 7:24good there and there's a character that
- 7:26can live anywhere so that's kind of a
- 7:27nice thing okay
- 7:30sizeof is an operator you're going to
- 7:31see used a lot later when we talk about
- 7:33reserving memory from up from preserving
- 7:36space from your memory system
- 7:38and size of any type is going to tell
- 7:39you how many bytes wide that type is
- 7:42so size of car is one by the definition
- 7:45of c999
- 7:46but remember you don't know how big size
- 7:47of events are so you actually can go to
- 7:49a new machine a great program to write
- 7:50on your first machine you're just
- 7:52you know encountering someone drops a
- 7:53new machine i just invented a new
- 7:54machine
- 7:55first thing you write is printf
- 7:56sizeofint how big are the ins there
- 7:58how big are the doubles how big are the
- 7:59longs that little table i showed you
- 8:01before
- 8:02had like long long long inch shorts do a
- 8:04whole table that says
- 8:05and this machine shorts are printf
- 8:09percent d size of short it's our
- 8:12print percent d size of ins and you keep
- 8:16doing this so actually be kind of neat
- 8:17to play with that as a
- 8:19as you as you move that as you ported we
- 8:20learned the word porting that code to
- 8:22different machines be kind of fun to see
- 8:23what the output of that was
- 8:25this is why in types exist so you have
- 8:27to kind of guess what the size of those
- 8:28guys are but sizeof is really useful to
- 8:30find out how big they are
- 8:31you can also pass in size of a struct
- 8:33type and a struct type says oh i make
- 8:35this struct that has an integer and it
- 8:37has a character
- 8:38that's four standard machine and this is
- 8:41five
- 8:41if i say sizeobstruct is going to tell
- 8:43me five or tell me eight
- 8:45does it actually make sure the struct is
- 8:47word aligned
- 8:48so does that mean even if i only use a
- 8:50character does it actually get a use up
- 8:51that whole int even though i only use
- 8:53one of those four bytes it's going to
- 8:54use a ball for because it has to be word
- 8:56aligned so
- 8:56you can ask size of an int plus a
- 9:00character to see whether it's
- 9:01they actually make sure you word align
- 9:02all of your structs as well okay
- 9:04we'll see size of a little bit later and
- 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.