Pointers in C 1 Prerequisite n Basics of
Pointers in C 1
Pre-requisite n Basics of the C programming language n n n Data type Variable Array Function call Standard Input/Output n e. g. printf(), scanf() 2
Outline Computer Memory Structure n Addressing Concept n Introduction to Pointer n Pointer Manipulation n Summary n 3
Computer Memory Revisited Computers store data in memory slots n Each slot has an unique address n Variables store their values like this: n Addr Content 1000 i: 37 1001 j: 46 1002 k: 58 1003 m: 74 1004 a[0]: ‘a’ 1005 a[1]: ‘b’ 1006 a[2]: ‘c’ 1007 a[3]: ‘ ’ 1008 ptr: 1001 1009 … 1010 1011 4
Computer Memory Revisited n Altering the value of a variable is indeed changing the content of the memory n e. g. i = 40; a[2] = ‘z’; Addr Content 1000 i: 40 1001 j: 46 1002 k: 58 1003 m: 74 1004 a[0]: ‘a’ 1005 a[1]: ‘b’ 1006 a[2]: ‘z’ 1007 a[3]: ‘ ’ 1008 ptr: 1001 1009 … 1010 1011 5
Addressing Concept Pointer stores the address of another entity n It refers to a memory location n int i = 5; int *ptr; /* declare a pointer variable */ ptr = &i; /* store address-of i to ptr */ printf(“*ptr = %dn”, *ptr); /* refer to referee of ptr */ 6
Why do we need Pointer? Simply because it’s there! n It is used in some circumstances in C n Remember this? scanf(“%d”, &i); 7
What actually ptr is? ptr is a variable storing an address n ptr is NOT storing the actual value of i n int i = 5; ptr address of i i 5 int *ptr; ptr = &i; printf(“i = %dn”, i); Output: printf(“*ptr = %dn”, *ptr); i = 5 printf(“ptr = %pn”, ptr); *ptr = 5 ptr = effff 5 e 0 value of ptr = address of i in memory 8
Twin Operators n &: Address-of operator n Get the address of an entity n e. g. ptr = &j; Addr Content 1000 i: 40 1001 1002 1003 1004 ptr: 1001 1005 j: 33 1006 k: 58 m: 74 1007 9
Twin Operators n *: De-reference operator n Refer to the content of the referee n e. g. *ptr = 99; Addr Content 1000 i: 40 1001 1002 1003 1004 ptr: 1001 1005 j: 99 1006 k: 58 m: 74 1007 10
Example: Pass by Reference n Modify behaviour in argument passing void f(int j) void f(int *ptr) { { j = 5; *ptr = 5; } } void g() { { } int i = 3; f(i); f(&i); i = 3? } i = 5? 11
An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; Data Table *ptr = 3; **pptr = 7; Name Type Description Value ptr = &j; i integer variable 5 **pptr = 9; j integer variable 10 *pptr = &i; *ptr = -2; 12
An Illustration int i = 5, j = 10; int *ptr; /* declare a pointer-to-integer variable */ int **pptr; ptr = &i; pptr = &ptr; Data Table *ptr = 3; **pptr = 7; Name Type Description Value ptr = &j; i integer variable 5 **pptr = 9; j integer variable 10 *pptr = &i; ptr int * integer pointer variable *ptr = -2; 13
An Illustration int i = 5, j = 10; int *ptr; int **pptr; /* declare a pointer-to-integer variable */ ptr = &i; pptr = &ptr; Data Table *ptr = 3; **pptr = 7; Name Type ptr = &j; i integer variable 5 **pptr = 9; j integer variable 10 *pptr = &i; ptr int * pptr int ** integer pointer variable *ptr = -2; Description Value integer pointer variable Double 14
An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; /* store address-of i to ptr */ pptr = &ptr; Data Table *ptr = 3; **pptr = 7; Name Type ptr = &j; i integer variable 5 **pptr = 9; j integer variable 10 *pptr = &i; ptr int * pptr int ** integer pointer variable *ptr = -2; *ptr int Description integer pointer variable de-reference of ptr Value address of i 5 15
An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; /* store address-of ptr to pptr */ Data Table *ptr = 3; **pptr = 7; Name Type ptr = &j; i integer variable 5 **pptr = 9; j integer variable 10 *pptr = &i; ptr int * integer pointer variable address of i pptr int ** integer pointer variable address of ptr *pptr int * value of ptr (address of i) *ptr = -2; Description de-reference of pptr Value 16
An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; Data Table *ptr = 3; **pptr = 7; Name Type ptr = &j; i integer variable 3 **pptr = 9; j integer variable 10 *pptr = &i; ptr int * integer pointer variable address of i pptr int ** integer pointer variable address of ptr *ptr = -2; *ptr int Description de-reference of ptr Value 3 17
An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; Data Table *ptr = 3; **pptr = 7; Name Type ptr = &j; i integer variable 7 **pptr = 9; j integer variable 10 *pptr = &i; ptr int * integer pointer variable address of i pptr int ** integer pointer variable address of ptr *ptr = -2; **pptr int Description de-reference of pptr Value 7 18
An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; Data Table *ptr = 3; **pptr = 7; Name Type ptr = &j; i integer variable 7 **pptr = 9; j integer variable 10 *pptr = &i; ptr int * integer pointer variable address of j pptr int ** integer pointer variable address of ptr *ptr = -2; *ptr int Description de-reference of ptr Value 10 19
An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; Data Table *ptr = 3; **pptr = 7; Name Type ptr = &j; i integer variable 7 **pptr = 9; j integer variable 9 *pptr = &i; ptr int * integer pointer variable address of j pptr int ** integer pointer variable address of ptr *ptr = -2; **pptr int Description de-reference of pptr Value 9 20
An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; Data Table *ptr = 3; **pptr = 7; Name Type ptr = &j; i integer variable 7 **pptr = 9; j integer variable 9 *pptr = &i; ptr int * integer pointer variable address of i pptr int ** integer pointer variable address of ptr *pptr int * value of ptr (address of i) *ptr = -2; Description de-reference of pptr Value 21
An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; Data Table *ptr = 3; **pptr = 7; Name Type ptr = &j; i integer variable -2 **pptr = 9; j integer variable 9 *pptr = &i; ptr int * integer pointer variable address of i pptr int ** integer pointer variable address of ptr *ptr = -2; *ptr int Description de-reference of ptr Value -2 22
Pointer Arithmetic n What’s ptr + 1? The next memory location! n What’s ptr - 1? The previous memory location! n What’s ptr * 2 and ptr / 2? Invalid operations!!! 23
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3. 14; ptr++; *ptr = 9. 0; ptr = ptr - 3; *ptr = 6. 0; ptr += 2; *ptr = 7. 0; Data Table Name Type a[0] float array element (variable) ? a[1] float array element (variable) ? a[2] float array element (variable) ? a[3] float array element (variable) ? ptr *ptr Description Value float * float pointer variable float de-reference of float pointer variable ? 24
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3. 14; ptr++; *ptr = 9. 0; ptr = ptr - 3; *ptr = 6. 0; ptr += 2; *ptr = 7. 0; Data Table Name Type a[0] float array element (variable) ? a[1] float array element (variable) ? a[2] float array element (variable) ? a[3] float array element (variable) ? ptr *ptr Description float * float pointer variable float de-reference of float pointer variable Value address of a[2] ? 25
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3. 14; ptr++; *ptr = 9. 0; ptr = ptr - 3; *ptr = 6. 0; ptr += 2; *ptr = 7. 0; Data Table Name Type a[0] float array element (variable) ? a[1] float array element (variable) ? a[2] float array element (variable) 3. 14 a[3] float array element (variable) ? ptr *ptr Description float * float pointer variable float de-reference of float pointer variable Value address of a[2] 3. 14 26
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3. 14; ptr++; *ptr = 9. 0; ptr = ptr - 3; *ptr = 6. 0; ptr += 2; *ptr = 7. 0; Data Table Name Type a[0] float array element (variable) ? a[1] float array element (variable) ? a[2] float array element (variable) 3. 14 a[3] float array element (variable) ? ptr *ptr Description float * float pointer variable float de-reference of float pointer variable Value address of a[3] ? 27
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3. 14; ptr++; *ptr = 9. 0; ptr = ptr - 3; *ptr = 6. 0; ptr += 2; *ptr = 7. 0; Data Table Name Type a[0] float array element (variable) ? a[1] float array element (variable) ? a[2] float array element (variable) 3. 14 a[3] float array element (variable) 9. 0 ptr *ptr Description float * float pointer variable float de-reference of float pointer variable Value address of a[3] 9. 0 28
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3. 14; ptr++; *ptr = 9. 0; ptr = ptr - 3; *ptr = 6. 0; ptr += 2; *ptr = 7. 0; Data Table Name Type a[0] float array element (variable) ? a[1] float array element (variable) ? a[2] float array element (variable) 3. 14 a[3] float array element (variable) 9. 0 ptr *ptr Description float * float pointer variable float de-reference of float pointer variable Value address of a[0] ? 29
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3. 14; ptr++; *ptr = 9. 0; ptr = ptr - 3; *ptr = 6. 0; ptr += 2; *ptr = 7. 0; Data Table Name Type a[0] float array element (variable) 6. 0 a[1] float array element (variable) ? a[2] float array element (variable) 3. 14 a[3] float array element (variable) 9. 0 ptr *ptr Description float * float pointer variable float de-reference of float pointer variable Value address of a[0] 6. 0 30
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3. 14; ptr++; *ptr = 9. 0; ptr = ptr - 3; *ptr = 6. 0; ptr += 2; *ptr = 7. 0; Data Table Name Type a[0] float array element (variable) 6. 0 a[1] float array element (variable) ? a[2] float array element (variable) 3. 14 a[3] float array element (variable) 9. 0 ptr *ptr Description float * float pointer variable float de-reference of float pointer variable Value address of a[2] 3. 14 31
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3. 14; ptr++; *ptr = 9. 0; ptr = ptr - 3; *ptr = 6. 0; ptr += 2; *ptr = 7. 0; Data Table Name Type a[0] float array element (variable) 6. 0 a[1] float array element (variable) ? a[2] float array element (variable) 7. 0 a[3] float array element (variable) 9. 0 ptr *ptr Description float * float pointer variable float de-reference of float pointer variable Value address of a[2] 7. 0 32
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); n n ptr = &(a[2]) ptr = &(*(a + 2)) ptr = a + 2 *ptr = 3. 14; ptr++; *ptr = 9. 0; ptr = ptr - 3; Type of a is float * a[2] *(a + 2) *ptr = 6. 0; n ptr += 2; n a is a memory address constant ptr is a pointer variable *ptr = 7. 0; 33
More Pointer Arithmetic What if a is a double array? n A double may occupy more memory slots! n n n Given double *ptr = a; What’s ptr + 1 then? Addr Content 1000 a[0]: 37. 9 1001 … 1002 … 1003 … 1004 a[1]: 1. 23 1005 … 1006 … 1007 … 1008 a[2]: 3. 14 1009 … 1010 … 1011 … 34
More Pointer Arithmetic operators + and – auto-adjust the address offset n According to the type of the pointer: n n 1000 + sizeof(double) = 1000 + 4 = 1004 Addr Content 1000 a[0]: 37. 9 1001 … 1002 … 1003 … 1004 a[1]: 1. 23 1005 … 1006 … 1007 … 1008 a[2]: 3. 14 1009 … 1010 … 1011 … 35
Advice and Precaution n Pros n n n Efficiency Convenience Cons n n Error-prone Difficult to debug 36
Summary A pointer stores the address (memory location) of another entity n Address-of operator (&) gets the address of an entity n De-reference operator (*) makes a reference to the referee of a pointer n Pointer and array n Pointer arithmetic n 37
- Slides: 37