Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS/COE 0449 Jarrett Billingsley

Similar presentations


Presentation on theme: "CS/COE 0449 Jarrett Billingsley"— Presentation transcript:

1 CS/COE 0449 Jarrett Billingsley
C – Pointers and Arrays CS/COE 0449 Jarrett Billingsley

2 fgets(input, sizeof(input), stdin); // do stuff with input, it's fine,
Class announcements project 1 due by this Saturday night! remember I have office hours Fridays 12-2 in 6509 SENSQ oh yeah: when getting input in a loop, you don't have to clear the input buffer char input[100]; while(1) { fgets(input, sizeof(input), stdin); // do stuff with input, it's fine, // you don't have to clear it out or // anything before the next loop } CS449

3 What are pointers? CS449

4 Memory! (déjà vu) memory is a big one-dimensional array of bytes
every byte value has an address this is its "array index" addresses start at 0, like arrays in C/Java for values bigger than a byte, we use consecutive bytes the address of any value, regardless of size, is the address of the first byte (smallest address) here is an int variable what is its sizeof()? what is its address? what is its value? :^) Addr Val ... DC0B 44 DC0A 4 DC09 2 DC08 88 DC07 BE DC06 EF DC05 C0 DC04 DE DC03 2A DC02 27 DC01 0E DC00 42 DC0D 8B DC0E BB - we often draw memory with the addresses increasing upwards – this will make more sense when we get to the stack - its sizeof() is 4 in this case - 4 bytes long - its address is 0xDC04 - its value could be either 0xDEC0EFBE (big-endian) or 0xC0DEBEEF (little-endian) CS449

5 5! x 5 Split personalities x
variables are not the same thing as the values they hold this variable x holds 5 but x is, itself, "a thing" we can do things with the variable, not just the value it holds it's like a car the car can have a name you can put things in it the car is its own thing though 5 x 5! x CS449

6 &x x Pointy things a pointer is like the key to the car
it's easy to hand the key around it's easy* to copy the key the key isn't the car but it lets you access the car and the things inside it and you can give the key to someone else but be careful who you give them to! &x x * stretching the metaphor a bit, but it's far easier to copy a key than it is to copy a car - you have to be careful when passing pointers around too, since someone might hang onto a copy of a pointer you didn't expect - and then they try to use it - and things go wrong CS449

7 Pointers in C CS449

8 You've kinda used pointers in Java...
remember writing linked lists? class Link { Link next; int value; } Link list = new Link(); list.next = new Link(); list.next.next = new Link(); next: value: 0 next: value: 0 next: value: 0 what about a reference that doesn't refer to anything? C has null too, but you have to yell it: NULL! - if you don't remember linked lists, welllllllll project 2 is gonna have them :^) CS449

9 int[] int* int[][] int**
Declaring pointers in Java, how do you declare an array of any type X? you put square brackets after the type: X[] int[] int* an array that holds ints. an pointer to an int. int[][] int** an array that holds arrays, and each of those holds ints. a pointer to a pointer, which points to an int. a C pointer can point to either a single value or an array of that type. - the meaning of the pointer – single value or array – depends on context. - really, non-array variables are just arrays of length 1… ;) - often, but not always, you can think of a pointer as pointing to an array of values… and a double-pointer is almost always a 2D array CS449

10 The address-of operator
when used as a prefix operator, & means "address of" it gives you the memory address of any variable, array item, etc. the address is given to you as a pointer type i.e. it "adds a star" I know it seems backwards, why wouldn't they make * add a star, or name pointers int& right? use it on an int? you get an int* use it on an int*? you get an int** YOU GET THE IDEA I hope you can use it on just about anything with a name &x &arr[10] &main (yep!) We'll talk about function pointers at a later date CS449

11 Pointers are variables too…
a variable is a named piece of memory a pointer is a variable that holds a memory address int x = 0x100; int y = 0x200; int* px = &x; int* py = &y; Name Address Value x DC00 0100 y DC04 0200 px DC08 DC00 py DC0C DC04 the addresses of these variables are given to us automatically by the compiler-ish since pointers are variables, can you get their addresses? - "ish" because when we learn about the stack, you'll see that the addresses of local variables can differ depending on where on the stack they are executing - but the compiler gives you the offsets of those variables from the stack pointer. - yes. yes, you can. CS449

12 Like a locker room, but without the awkward nudity
every variable is a locker each locker has a number its address! and each locker has its contents its value! and one locker can hold the number of another locker a pointer! float 3.14 locker 25 - this is a better metaphor than the car, huh - especially since lockers can have keys too… - next time I teach it guess CS449

13 Accessing the value(s) at a pointer
CS449

14 The value-at (or "dereference") operator
* is the value-at operator it dereferences a pointer that is, it accesses the memory that a pointer points to it's the inverse of & every time you use it, you remove a star again, this feels backwards? int** ppx = ... int* px = *ppx; int x = *px; goes to the address that ppx contains, and gets the int* there goes to the address that px contains, and gets the int there - you might think of it like a little breadcrumb… it's the "follow the breadcrumb" operator (look up "Hansel and Gretel") - so an int** is two breadcrumbs that lead you to an int… - idk I thought it was cute CS449

15 Follow the breadcrumbs (animated)
now let's see what happens when we dereference these pointers: int x = 0x100; int y = 0x200; int* px = &x; int* py = &y; *px = 0x300; printf("%x\n", x); printf("%x\n", *px); Name Address Value x DC00 0100 y DC04 0200 px DC08 py DC0C 0300 these two lines read the same value from the same location in memory! - since they read the same value, both printfs will print 0x300. - really, a regular variable is a bit like an "automatic pointer"… - the compiler knows the address, and whenever you read from or write to a variable, it changes the value in memory at that address - pointers just give us a way to do it manually, on our own terms CS449

16 Watch out.. (animated) int x = 0x100; int* px = &x; px = 0x300;
What does this program do? int x = 0x100; int* px = &x; px = 0x300; printf("%x\n", x); printf("%x\n", *px); Name Address Value x DC00 0100 px DC04 0300 - the second printf WILL CRASH – it will try to go to address 0x0300, which will, in almost any case, be an invalid address - here's an example of a pointer which is neither null nor valid! CS449

17 Another way of accessing the value at a pointer
arrays are just pointers. char buffer[100]; printf("%p\n", buffer); // oh. when you write array[i], what's happening? well, let's use the address-of operator to help us see... when you use the array indexing operator, you're really just adding an offset to the pointer, and using that as the address to access. a[i] is 100% interchangeable with *(a + i). we can do math on pointers… this is called pointer arithmetic. - the %p specifier prints the pointer's value in hex. - see 5_arrays_are_pointers.c CS449

18 Pointer arithmetic array[3] DC0C 3 array[2] DC08 2 array[1] DC04 1
if we write this: int array[] = {0, 1, 2, 3}; memory looks like this: if we want to access array[2]... what is that equivalent to? *(array + 2) but how big is each item in the array? (what is sizeof(int)?) when we write array + 2, we don't get 0xDC02, we get 0xDC08 it adds the size of 2 items to the address when you add or subtract offsets to pointers, C "scales" the offsets by multiples of the size of the type they point to. Name Address Value array[3] DC0C 3 array[2] DC08 2 array[1] DC04 1 array[0] DC00 - each item is, in this case, 4 bytes long (sizeof(int) == 4) - keep in mind that array indexes and byte addresses are different things!! - this is why C has to know what a pointer points to – so it can do this math correctly. (in addition to catching bugs. (sorta.)) CS449

19 Oh yeah, and that stupid -> operator
if you have a pointer to a struct, you must access its fields with -> Food* pgrapes = &produce[0]; pgrapes->price = 2.99; (*pgrapes).price = 2.99; these are identical in meaning. - go look at 4_food_struct.c again! CS449

20 Common pointer patterns
CS449

21 Every problem in CS... ...can be solved with another level of indirection/references/pointers. pointers are the basis of: strings arrays object-oriented programming dynamic memory management pretty much everything your operating system does pretty much everything... everything does. higher level languages often give you more abstract, safer ways of achieving the same things that you can do with pointers CS449

22 Multi-dimensional arrays
we already saw single-dimensional arrays, but… int** arr2d = ... 1 9 4 3 7 6 a Java int[][] works exactly the same way! 2 5 3 9 6 4 3 7 1 2 3 4 5 6 7 - THIS IS ACTUALLY COMPLETELY DIFFERENT FROM AN "int arr[4][6];" OR SOMETHING - see 5_2d_arrays.c for a comparison CS449

23 function_that_returns_two_values(&x, &y);
Pass-by-reference often you want to give another function access to your variables. fgets(buffer, 100, stdin); int x, y; function_that_returns_two_values(&x, &y); since these functions have access to buffer, x, and y, they can change their values. - again, see 4_struct_food.c for a function that takes a Food by reference! CS449


Download ppt "CS/COE 0449 Jarrett Billingsley"

Similar presentations


Ads by Google