Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers Discussion 5 Section 0104. Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!

Similar presentations


Presentation on theme: "Pointers Discussion 5 Section 0104. Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!"— Presentation transcript:

1 Pointers Discussion 5 Section 0104

2 Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!

3 Pointers A pointer is a Memory address/ reference Declare a ptr to an int would be int * ip; - Creates a variable called ip which is a pointer to int Every time you write int x; C creates space in memory for it Get the address of something by (&) Ex: int y; int * x = &y; Access the object using (*) Ex: int x = 6; int * y = &x; int k = *y; Called “dereferencing”

4 * is very special with Pointers On the left hand side of =, it means a ptr type. Ex: int * x; char * y, double * z These are all pointers! On the right hand side of =, it means dereferencing Ex: int x; int * ptr; x = *ptr Here, the * is not referencing to a pointer type, but is accessing the value that ptr points to.

5 Pointers Are Undeclared What happens if you do: int * x; int y = *x; x points to an unknown location. So, dereferencing x has unknown behavior -Causes segmentation faults -y = some garbage value -Silent Errors -Alignment policies

6 sizeof() Returns the size in bytes that the variable is taking int x; char array [5]; sizeof(int) returns 4 because an int is 4 bytes sizeof(x) returns 4 because x is an int which is 4 bytes. sizeof(array) returns 6 because it is 5 elements long + \o character sizeof(ptr) returns 8 because the address is stored as 8 bytes.

7 Common Uses for Pointers FILE * is actually a ptr! Arrays are pointers. If a char * is a parameter, you can also pass in an array of chars Arrays are pointers to the first element of an array (int array [5]) array [0] means the first element in the array array[5] means it goes to the pointer at the first element and then moves over 5 int spaces. Strings are pointers Valid: char str[6]; char * strptr = str; What is the sizeof(str) and sizeof(strptr)? scanf takes a pointer. scanf(“Enter an int: %d”, &x)

8 Ptr Example 1 int x = 3; int *ptr; ptr = &x; printf(“%d %d”, *ptr, *(&x)); *ptr = 5;

9 Ptr Example 2 int x = 3; int *ptr; ptr = &x; ptr = NULL; x = *ptr; What will happen?

10 Pointers to Pointers Can you have pointers to pointers? YES int x = 5; int y = 6; int *ptr1 = &x; int *ptr2 = &y; int ** ptr3 = &ptr1; //double pointer printf(“%d”, **ptr3); //prints 5 *ptr3 = ptr2; printf(“%d”, **ptr3); //prints 6 **ptr3 = 2; printf(“%d”, *ptr1); printf(“%d”, *ptr2);

11 Pointers to Pointer Example char * argv [] is a pointer to a pointer. It is an array of char * Remember that an array is just a pointer to the first element So, a char array is the same as a char *

12 Passing Pointers as parameters void swap(int x, int y){ int temp = x; x = y; y = temp; } int main(){ int x = 4; int y = 10; swap(x, y); printf(“%d %d”, x, y); } Does this work?

13 Passing Pointers as parameters Cont. void swap(int* xptr, int* yptr){ int temp = *xptr; *xptr = *yptr; *yptr = temp; } int main(){ int x = 4; int y = 10; swap(&x, &y); printf(“%d %d”, x, y); }

14 Pointer Arithmetic Only two operations Distance between two pointers ptr1 – ptr2 Moving a pointer up a certain amount (No Safety) ptr1 + k ptr1 – k Where k is an integer ptr1++


Download ppt "Pointers Discussion 5 Section 0104. Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!"

Similar presentations


Ads by Google