Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS/COE 0449 (term 2174) Jarrett Billingsley

Similar presentations


Presentation on theme: "CS/COE 0449 (term 2174) Jarrett Billingsley"— Presentation transcript:

1 CS/COE 0449 (term 2174) Jarrett Billingsley
C - Pointers CS/COE 0449 (term 2174) Jarrett Billingsley

2 Class announcements Project 1! Due at 11:59 PM on Sunday, February 5th
Quiz 1 will be next Tuesday!! Yeah, that's right! Midterm 1 is in two weeks!!! Boy, you've got a lot of stuff to get ready for, huh? I really recommend you do the practice exercises in the book... The labs this week and next week will also give you more practice with C. 1/19/2017 CS/COE term 2174

3 Question time! 1/19/2017 CS/COE term 2174

4 Pointers 1/19/2017 CS/COE term 2174

5 Memory! Memory's a big array of bytes. Every byte has an address.
The only way your computer can access any data in memory (all the variables etc) is by knowing their addresses. Say we had a variable int x and its address was DC04. How many bytes would it span? In a little-endian machine, what would its value be? (you're learning about endianness in 447, right?) 0xBEEFC0DE! Address Value ... 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 1/19/2017 CS/COE term 2174

6 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(); Object references are really just pointers. So if you start getting confused, just think about how those work. next: value: 0 next: value: 0 next: value: 0 1/19/2017 CS/COE term 2174

7 int x = 0x100; int y = 0x200; int* px = &x; int* py = &y;
Variables vs. Pointers 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 py DC0C DC04 px DC08 DC00 y 0200 x 0100 1/19/2017 CS/COE term 2174

8 Declaring pointers In Java, you can make an array of any type by sticking brackets after it. int[], int[][], int[][][] etc. In C, you can make a pointer to any type by sticking stars after it. int*, int**, int*** etc. When you make a pointer, the type before the star is the type of value that it points to. So an int* can point to an int variable. 1/19/2017 CS/COE term 2174

9 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. Use it on an int? You get an int*. Let's try printing out addresses using the %p format specifier with printf! 1/19/2017 CS/COE term 2174

10 The address-of operator
Coming back to this example... int x = 0x100; int y = 0x200; int* px = &x; int* py = &y; What value does &x give us? What type is &x? What value does &px give us? What type is &px? OHOHOHOHO Name Address Value py DC0C DC04 px DC08 DC00 y 0200 x 0100 1/19/2017 CS/COE term 2174

11 The dereference operator
An address on its own isn't that useful... What's useful is being able to read and write the data at that address. When used as a prefix operator, * is the dereference operator. When you use it on a pointer, it's like you're directly using the variable that it points to. 1/19/2017 CS/COE term 2174

12 The dereference operator
Okay: 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 py DC0C DC04 px DC08 DC00 y 0200 x 0100 0300 1/19/2017 CS/COE term 2174

13 Watch out.. 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 px DC04 DC00 x 0100 0300 1/19/2017 CS/COE term 2174

14 Everyone on the same page?
1/19/2017 CS/COE term 2174

15 So what? 1/19/2017 CS/COE term 2174

16 Every problem in CS... ...can be solved with another level of indirection (pointers). Pointers are the basis of: Dynamic memory management Strings Arrays Object-oriented programming Pretty much everything your operating system does Everything... everything does. Understanding pointers is understanding how software works. 1/19/2017 CS/COE term 2174

17 Functions Functions are machines which grind up socks and gloves and turn them into numbers. ?????????????? Very important diagram from the book here. 1/19/2017 CS/COE term 2174

18 Passing by reference In Java, you're stuck returning at most one value unless you resort to ugly hacks like returning an Object[]. In C, you can use pointers to modify values given as parameters. Let's write a function that takes two pointers – one for hours and one for minutes – and increments the minutes by 1. If the minutes exceed 59, then they should reset to 0 and tick the hours up by 1. 1/19/2017 CS/COE term 2174

19 Arrays! 1/19/2017 CS/COE term 2174

20 No more beating around the bush
I've been saying in C, arrays are Not A Thing. That's because... As far as C concerned, an array is just a pointer. Wat. char buffer[40]; printf("%p\n", buffer); // woah. 1/19/2017 CS/COE term 2174

21 What does the [] operator do?
When you write array[i], what's happening? Well, let's use the address-of operator to help us see... 1/19/2017 CS/COE term 2174

22 Pointer arithmetic 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. In other words, a[i] is interchangeable with *(a + i). Wait, but— If a is a pointer, what does a + i mean? Let's print it out. 1/19/2017 CS/COE term 2174

23 Pointer arithmetic array[3] DC0C 0003 array[2] DC08 0002 array[1] DC04
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 look at the addresses. How big is each item in the array? When we write array + 2, it doesn't add 2 to the address. We don't get 0xDC02. It adds the size of 2 items to the address. We get 0xDC08. Name Address Value array[3] DC0C 0003 array[2] DC08 0002 array[1] DC04 0001 array[0] DC00 0000 1/19/2017 CS/COE term 2174

24 Arrays-of-arrays? Before we go, I will leave you with this:
int main(int argc, char** argv) 1/19/2017 CS/COE term 2174


Download ppt "CS/COE 0449 (term 2174) Jarrett Billingsley"

Similar presentations


Ads by Google