[CS61C FA20] Lecture 04.3 - C Intro: Pointers, Arrays, Strings: Arrays — Transcript
Full transcript
- 0:00and welcome back let's now talk about
- 0:03c arrays so arrays
- 0:07are similar to what you've probably seen
- 0:09in java you
- 0:10declare them by saying what type it is
- 0:12and how many you want
- 0:14so here the first line says int ar of 2
- 0:16says
- 0:17i want two integers side to side the
- 0:20most important thing you understand
- 0:21about arrays is that
- 0:22it's a way of asking c for a contiguous
- 0:25block of memory
- 0:26i need to have two integers side by side
- 0:28you can't have them here with a gap in
- 0:30the middle you have them together
- 0:32you can also declare and initialize them
- 0:34at the same time without saying the
- 0:35space so
- 0:36ar open square back open close square
- 0:39bracket equals
- 0:40two numbers um initializes those two
- 0:43spots and says there's gonna be two of
- 0:44them
- 0:44and the way you access them is the
- 0:46square bracket notation you've seen from
- 0:47python
- 0:48and from java ar of num or num is some
- 0:50value
- 0:51and it's zero index it's really
- 0:52important
- 0:54so mostly arrays and pointers are
- 0:56identical actually
- 0:58they're mostly the same uh so if i say
- 0:59care star string
- 1:01or care string open square bracket the
- 1:03first says the string
- 1:05is a pointer to a character array to a
- 1:08i even say character array um and the
- 1:10second says that
- 1:12string is an array so first a string is
- 1:14a pointer
- 1:15to a contiguous block of characters
- 1:19and the second is that string is a
- 1:20character array the neural identical
- 1:23people often almost the same almost
- 1:25always even confuse them in the way they
- 1:26describe them
- 1:28what's different is that you cannot
- 1:29increment uh
- 1:31an array the open square bracket you
- 1:32can't increment it you can't move the
- 1:34pointer you can for the pointer but you
- 1:35can't this so if you have a big array
- 1:37and you're
- 1:37moving across you can't increment it if
- 1:39you have open square brackets
- 1:40that's really the only it's very subtle
- 1:42the way you declare declare a filled
- 1:44array you'll
- 1:44only do it with the guy on the right not
- 1:46the guy on the left but you can think of
- 1:48it
- 1:48as an array variable points to the first
- 1:50element i mean there's they're also
- 1:51subtle some other ways of where they're
- 1:53stored and i'll talk about that in a
- 1:54second but
- 1:55that's the big idea okay so mostly
- 1:56they're the same i'll say they're the
- 1:57same
- 1:59what that means is if you have ar and
- 2:01it's an array variable
- 2:04but it looks like a pointer in most
- 2:05respects so you can say ar of 0
- 2:07you can also say star ar remember ar was
- 2:09declared as an array
- 2:11you can still say star ar you can also
- 2:13say ar of 0.
- 2:15you can say ar of 2 you can also say
- 2:17star
- 2:18ar plus 2. this is the first time i've
- 2:20shown you pointer arithmetic and the
- 2:22plus two says
- 2:23look whatever ar is pointing to i don't
- 2:25want that one the zeroth element
- 2:27i don't want the next one that's the
- 2:29first element well it's not the first
- 2:30it's the one element
- 2:31index i want the two index elements one
- 2:34two over
- 2:35so you say well dan how does it know how
- 2:36wide they are how how much to increment
- 2:38the pointer
- 2:39to increment that um so incrementing the
- 2:42point
- 2:42how how far to go across to get that
- 2:44value uh
- 2:45that's what types are about that's why
- 2:47you told it what type that
- 2:48was that pointer was two that array was
- 2:50for um
- 2:53they're only the declared space of an
- 2:55array is only valid they're temporary
- 2:57while the scope of that procedure is
- 2:59valid so here's
- 3:00care starfu and make a new character and
- 3:02make a new subroutine called foo
- 3:04pretty cool i'm going to return i'm
- 3:06going to return uh
- 3:07a pointer and then i say character of
- 3:10string of 32 i'm making now some local
- 3:12storage
- 3:13uh array of 32 32 characters calling it
- 3:17string
- 3:18and i'm going to return that you're not
- 3:20supposed to do that because
- 3:21that space was only available in that
- 3:24subroutine
- 3:25so only what foo was live there was that
- 3:27a string available and the moment foo
- 3:29ends
- 3:29the system might reuse that so you don't
- 3:31have access that's a
- 3:32key error we're going to mention that
- 3:34many more times but don't
- 3:35do that don't do what we did here in foo
- 3:37you don't have access to that once the
- 3:39procedure subroutine returns
- 3:43i mentioned before the zero index so if
- 3:45the race size is n you want to access
- 3:46zero to n minus one that's your range of
- 3:48things you can get to
- 3:49the problem is this particular slide is
- 3:53talking about
- 3:55a style thing this is really a style
- 3:56thing here so this the top line of code
- 3:59looks right like doesn't this look right
- 4:00look at it
- 4:01into i are of 10 for i
- 4:04is 0 less than 10 i plus plus looks good
- 4:06right
- 4:08well the problem is you've written 10
- 4:10you've kind of hard coded 10
- 4:12twice and what we want to teach you with
- 4:15621c is not just
- 4:16how c works but actually good style and
- 4:18so
- 4:19one of the things we want to make sure
- 4:21inculcate in this class
- 4:23is that you understand that the single
- 4:25source of truth is a valuable thing
- 4:27that's the reason why google drive and
- 4:29google
- 4:30documents are so great you have a single
- 4:31source of truth and 25 people are doing
- 4:33it rather than passing out
- 4:34version final final final no version and
- 4:37oh version 17 right
- 4:38crazy we look back and we ask ourselves
- 4:40why do we ever do that
- 4:42same thing here and see once you realize
- 4:44that you're going to have 10 hard-coded
- 4:46you should
- 4:46the hair in the back of your neck should
- 4:48go up i should say you know what i
- 4:49shouldn't do this how do i do this
- 4:50better well one way to do is you say int
- 4:52array size is 10
- 4:53before and ansi days before cnn you have
- 4:56to have pound signed
- 4:57array size 10. um pound size pound sign
- 5:00define
- 5:01array size 10. you have to def global
- 5:03define this as a as
- 5:04a pound defined in a c preprocessor
- 5:06directive
- 5:08here int array size equals 10. so into i
- 5:11a of array size this is a dynamic array
- 5:13again this came from c9
- 5:15and now i can say for i equals zero i'd
- 5:17list an array size
- 5:18now i only have one ten if i want to now
- 5:20change it
- 5:21i want to make a 20. now i change it one
- 5:23place so look at this and
- 5:25you know look well young padawan as you
- 5:27understand the
- 5:28the ways that that the pro tip for how
- 5:31to do this you want to really have any
- 5:33hard-coded value have it there once
- 5:35abstraction okay so use indirection
- 5:37indirection can save you a lot of
- 5:38trouble
- 5:39and i mean don't have to maintain two
- 5:40copies of 10.
- 5:43here's the other thing arrays don't know
- 5:46when their bounds are being exceeded
- 5:47this is c
- 5:48if there's ever a question of whether c
- 5:49checks or something the answer is always
- 5:51no
- 5:51c doesn't do any of those checks c says
- 5:53i'm just gonna let you run as fast as
- 5:54you want because basically i'm gonna
- 5:56compile it down to raw machine code i'm
- 5:57not gonna do those checks for you those
- 5:59sanity checks or make sure you're
- 6:00between zero and n minus one
- 6:02no doesn't know that the other downside
- 6:04is
- 6:05an array doesn't carry with it it's its
- 6:07size
- 6:08so that's a little bit of a bummer so if
- 6:09you ever pass an array around
- 6:11you have to pass the array and you got
- 6:13to pass the size of the array
- 6:15um or at least the number that you're
- 6:16going to be searching up to you can pass
- 6:18it you know a rate of size 100 if you
- 6:19only pass
- 6:2010 and then only think it's actually
- 6:21only 10 long and it won't go past that
- 6:23but you you need to kind of pass these
- 6:24two numbers along um there's only one
- 6:27small
- 6:28um asterisk to it exception which is
- 6:30strings
- 6:31because we've all agreed that zero
- 6:33character is a special way to terminate
- 6:34a string
- 6:35so if i'm passing a string to you you
- 6:37can keep reading until you get to zero
- 6:39you don't have to pass the string and
- 6:40the number now you can also write the
- 6:41number and then you
- 6:42you know you'll stop short of that but
- 6:44strings you don't have to pass because
- 6:45we've all agreed that zero
- 6:46is a terminate a null terminator at the
- 6:48end of a string but every other array
- 6:51every other array you have to pass in
- 6:52the array and the size or at least the
- 6:54size
- 6:55up two you want to be checking okay
- 6:57you're going to see some errors
- 6:58as you're coding in c you wouldn't be
- 7:01learning if you weren't finding irish
- 7:02you know you know if you're
- 7:03you're not learning how to ride a
- 7:04bicycle if you don't scrape your knee
- 7:05once or twice you've got to kind of
- 7:06try whatever if i wiggle the wheels what
- 7:08if i turn really steep well you hurt
- 7:10yourself and you're following but if i
- 7:11pedal when i'm making it
- 7:12i did this pedaling while i'm making a
- 7:14turn all of a sudden that pedal hits the
- 7:16drawer yeah
- 7:17so you got to try all these things to
- 7:18know what the bounds and range is so
- 7:19that when you get to a dangerous
- 7:20situation you won't know to do that
- 7:21you won't do that so
- 7:25segmentation faults and bus arrows are
- 7:26two kinds of errors that you'll see
- 7:28the idea of i'll just give you a little
- 7:30quickie um a segmentation fault says
- 7:33you're reading and writing to memory you
- 7:34don't have access to
- 7:35you're reading outside the segment the
- 7:37segment kind of is the way that your
- 7:38your access space a bus error often
- 7:41means your alignment is wrong
- 7:43so you know you want to talk about word
- 7:45alignment a couple of videos ago
- 7:46i'm going to read an integer if your
- 7:48integer pointer
- 7:50doesn't have if you say what's the
- 7:51address of that of that of that integer
- 7:53if that integer isn't the address if you
- 7:56look at it in hex isn't 0
- 7:574 8 or c you're not word aligned that's
- 8:00in a 32-bit machine so then if you read
- 8:03an integer i'm reading an integer but
- 8:04it's but the actual pointer is
- 8:055 well the actual you know the actual
- 8:07address of that integer is 5 well that's
- 8:10not that's not 4 eight you know 12 or
- 8:12not a multiple of four so that would be
- 8:14a bus there
- 8:15okay if it's if it's in the space over
- 8:16there you could have it could be
- 8:18ambiguous where i read it off alignment
- 8:19but i'm also
- 8:20off the area i'm supposed to be reading
- 8:21you can have one of the two one of the
- 8:23two so
- 8:23those are two ways if you see those
- 8:25arrows that's what they mean
- 8:26segmentation fault means i'm outside of
- 8:28memory i have control the buzzer and
- 8:29he's probably not aligned
- 8:32i mentioned before point arithmetic
- 8:34which is um if you have a pointer to
- 8:36something and you say plus a number i
- 8:37mentioned star a r plus two goes to the
- 8:39second
- 8:40uh element in that array however big
- 8:43they are it just moves over just
- 8:44strides however big they are that's what
- 8:46size was four size of was four
- 8:49so pointer plus n says well if it's a
- 8:51pointer to something
- 8:53um it adds here's the thing
- 8:56size of character is one so it's n times
- 8:59the size of the character so it says i'm
- 9:00going to move
- 9:01that many bytes across to the next guy
- 9:03so if it's an integer four
- 9:04if i say pointer plus one a pointer is a
- 9:06thousand say point is a nice multiple of
- 9:07four
- 9:08point thousand point plus one uh this
- 9:11means now the pointer is going to be if
- 9:12i ask address it's a thousand and four
- 9:14order plus one again it'll be a thousand
- 9:15and eight so it's moving by the size of
- 9:17that element
- 9:18so that's why we have to use that's why
- 9:19those types are useful to know how
- 9:21big to move when you say pointer plus or
- 9:23minus point or minus means go backwards
- 9:25go back in the array so put a plus or
- 9:26point of minus does that kind of be
- 9:28useful thing
- 9:29so you saw this review we saw this a
- 9:31couple uh lecture maybe last lecture
- 9:33the way you if i have a subroutine add
- 9:36one and i have y
- 9:37declared outside that scope and i pass
- 9:39in y y is still
- 9:40y is three initially add one is not
- 9:42going to affect y it's still going to be
- 9:44three after the call to add one
- 9:46the way you fix it is you pass in the
- 9:48address of y
- 9:49and now that pointer can manipulate y
- 9:51and when it's all done y is now four
- 9:53that's all review
- 9:55what how about what if i want to change
- 9:56a pointer so let's look at this code
- 9:58here let me pull this out
- 9:59and see if i got this here okay
- 10:02so i've got increment pointer increment
- 10:05pointer here all right what color my
- 10:07drawing here
- 10:08increment pointer son of a gun
- 10:14there okay
- 10:19i have increment pointer
- 10:23what does it do well let's look at it
- 10:25let's start here
- 10:26it's supposed to move the pointer across
- 10:28so here's a of 3
- 10:30okay make a nice filled array by the way
- 10:32you can also i didn't mention
- 10:33before you can fill the array and also
- 10:35tell it how big it is
- 10:37as long as they're consistent nothing
- 10:39critical so 50 60 70 in this nice array
- 10:41i have a pointer q we can think of q and
- 10:44a both point to the beginning of that
- 10:45array so here's my picture over here
- 10:47indicating that then i called increment
- 10:49q increment pointer of q
- 10:51i really want q to move over so i want q
- 10:54to be here
- 10:54and point to there that's what what i
- 10:56want is for this
- 10:58there to be my q okay so let's now call
- 11:00this well what would it do
- 11:02in star p i pass in the pointer to p now
- 11:04p and q
- 11:05are the same thing and i say p equals p
- 11:07plus one and i hope that p
- 11:09that q increments okay well
- 11:13what do you think happens let me try
- 11:14this erase this then what happens
- 11:1750 doesn't move at all why
- 11:21same reason as the slide before you
- 11:23can't change
- 11:25q without passing its that's all on
- 11:28three one two three
- 11:29address so you pass in queue's
- 11:32address well now what is the type for
- 11:36the increment pointer that's receiving
- 11:37that
- 11:38it's not a pointer to a int it's a
- 11:41pointer
- 11:42to a pointer to an int actually h and by
- 11:45the way we call it h as we call this
- 11:47a handle so it's a pointer to a pointer
- 11:49h is a handle
- 11:51so now it gets pretty let's try it so
- 11:54now
- 11:54q i pass in the address of q now i can
- 11:58change q
- 11:59star star h the way you get to that the
- 12:01way you dereference that handle
- 12:02is you say star h this is going to move
- 12:06where
- 12:06q is one over so let's try this
- 12:10i run this q moves one over and i print
- 12:13f star q so this is the same
- 12:15this didn't change down here the printf
- 12:16didn't change on there it's just that
- 12:18key thing was different
- 12:20and that key different was different i
- 12:22also changed this as well
- 12:23okay so now same idea and you're gonna
- 12:26say well dan look
- 12:27if i want what if i have a pointer to a
- 12:28pointer to a point the answer is
- 12:30yes i'm gonna short circuit you and say
- 12:32you can have a pointed to a pointer to a
- 12:34pointer
- 12:34three stars pointed to a pointer to a
- 12:36pointer to a pointer four stars
- 12:38you typically don't have more than two
- 12:39stars in the code and if you have three
- 12:42either you know what you're doing it's
- 12:43really complicated code or
- 12:45you probably don't need to have it so
- 12:46think about trying to remove that if you
- 12:48can but
- 12:49it gets more complicated once you have
- 12:50three layers but people have handles all
- 12:52the time
- 12:53which is a reasonable thing to do we'll
- 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.