Pointers
Topics Introduction to pointers Pointers and function parameters Pointers Arithmetic
Memory Address of a Variable char ch = A ; ch: 0x000 A The memory address of the variable ch The value of the variable ch 3
The & Operator Gives the memory address of an object char ch = A ; 0x000 A &ch yields the value 0x000 Also known as the address operator 4
Example: char ch; printf( %p, &ch); conversion specifier for printing a memory address 5
Pointers A variable which can store the memory address of another variable 0x3A5 0x000 chptr 0xFFE 0xFFF 0x000 0x00 0x00 B ch etc 6
Pointers A pointer is a variable Contains a memory address Points to a specific data type 7
Example: cptr: char* cptr; 0x004 Can store an address of variables of type char We say cptr is a pointer to char 8
Pointers and the & Operator Example: char c = A ; char *cptr; cptr = &c; Assigns the address of c to cptr c: cptr: A 0x000 0x000 0x004 9
Notes on Pointers We can have pointers to any data type Example: int* numptr; float* xptr; The * can be anywhere between the type and the variable Example: int *numptr; float * xptr; 0
Notes on Pointers (cont) You can assign the address of a variable to a compatible pointer using the & operator Example: int anumber; int *numptr; numptr = &anumber; You can print the address stored in a pointer using the %p conversion specifier Example: printf( %p, numptr);
The * Operator Allows pointers to access variables they point to Also known as dereferencing operator Should not be confused with the * in the pointer declaration
Pointers and the? Operator Example: char c = A ; char *cptr = NULL; cptr = &c; *cptr = B ; Changes the value of the variable which cptr points to c: cptr: BA NULL 0x000 0x000 0x004 3
Easy Steps to Pointers Step : Declare the variable to be pointed to int num; char ch = A ; float x; num: ch: A x: 4
Easy Steps to Pointers (cont) Step : Declare the pointer variable int num; char ch = A ; float x; int* numptr = NULL; char *chptr = NULL; float * xptr = NULL; numptr: chptr: xptr: num: ch: x: NULL NULL NULL A 5
Easy Steps to Pointers (cont) Step 3: Assign address of variable to pointer int num; char ch = A ; float x; int* numptr = NULL; char *chptr = NULL; float * xptr = NULL; numptr = # chptr = &ch; xptr = &x; numptr: addr of num chptr: xptr: num: ch: addr of ch addr of x A A pointer s type has to correspond to the type of the variable it points to x: 6
Easy Steps to Pointers (cont) Step 4: De-reference the pointers int num; char ch = A ; float x; int* numptr = NULL; char *chptr = NULL; float * xptr = NULL; numptr = # chptr = &ch; xptr = &x; *xptr = 0.5; *numptr = *chptr; numptr: addr of num chptr: xptr: addr of ch addr of x num: 65 ch: A x: 0.5 7
Notes on Pointers (cont) int *numptr; Beware of pointers which are not initialized!??? numptr 8
Notes on Pointers (cont) When declaring a pointer, it is a good idea to always initialize it to NULL (a special pointer constant) int *numptr = NULL; NULL numptr 9
Pointers and Function Parameters Example: Function to swap the values of two variables x: y: swap x: y: 0
#include <stdio.h> Bad swap void swap(int a, int b) int tmp; tmp = a; a = b; b = tmp; return; int main() int x =, y = ; swap(x, y); printf( %d %d\n, x, y); return 0;
#include <stdio.h> Bad swap void swap(int a, int b) int tmp; tmp = a; a = b; b = tmp; return; int main() int x =, y = ; swap(x, y); printf( %d %d\n, x, y); return 0; x: y:
#include <stdio.h> Bad swap void swap(int a, int b) int tmp; tmp: tmp = a; a = b; b = tmp; return; a: b: int main() int x =, y = ; swap(x, y); printf( %d %d\n, x, y); return 0; x: y: 3
#include <stdio.h> void swap(int a, int b) int tmp; tmp: Bad swap tmp = a; a = b; b = tmp; return; a: b: int main() int x =, y = ; swap(x, y); printf( %d %d\n, x, y); return 0; x: y: 4
#include <stdio.h> void swap(int a, int b) int tmp; tmp: Bad swap tmp = a; a = b; b = tmp; return; a: b: int main() int x =, y = ; swap(x, y); printf( %d %d\n, x, y); return 0; x: y: 5
#include <stdio.h> void swap(int a, int b) int tmp; tmp: Bad swap tmp = a; a = b; b = tmp; return; a: b: int main() int x =, y = ; swap(x, y); printf( %d %d\n, x, y); return 0; x: y: 6
#include <stdio.h> void swap(int a, int b) int tmp; tmp: Bad swap tmp = a; a = b; b = tmp; return; a: b: int main() int x =, y = ; swap(x, y); printf( %d %d\n, x, y); return 0; x: y: 7
#include <stdio.h> Good swap void swap(int* a, int* b) int tmp; tmp = *a; *a = *b; *b = tmp; return; int main() int x =, y = ; swap(&x, &y); printf( %d %d\n, x, y); return 0; 8
#include <stdio.h> Good swap void swap(int* a, int* b) int tmp; tmp = *a; *a = *b; *b = tmp; return; int main() int x =, y = ; swap(&x, &y); printf( %d %d\n, x, y); return 0; x: y: 9
#include <stdio.h> Good swap void swap(int* a, int* b) int tmp; tmp: tmp = *a; *a = *b; *b = tmp; return; a: b: addr of x addr of y int main() int x =, y = ; swap(&x, &y); printf( %d %d\n, x, y); return 0; x: y: 30
#include <stdio.h> void swap(int* a, int* b) int tmp; tmp: Good swap tmp = *a; *a = *b; *b = tmp; return; a: b: addr of x addr of y int main() int x =, y = ; swap(&x, &y); printf( %d %d\n, x, y); return 0; x: y: 3
#include <stdio.h> void swap(int* a, int* b) int tmp; tmp: Good swap tmp = *a; *a = *b; *b = tmp; return; a: b: addr of x addr of y int main() int x =, y = ; swap(&x, &y); printf( %d %d\n, x, y); return 0; x: y: 3
#include <stdio.h> void swap(int* a, int* b) int tmp; tmp: Good swap tmp = *a; *a = *b; *b = tmp; return; a: b: addr of x addr of y int main() int x =, y = ; swap(&x, &y); printf( %d %d\n, x, y); return 0; x: y: 33
#include <stdio.h> Good swap void swap(int* a, int* b) int tmp; tmp = *a; *a = *b; *b = tmp; return; int main() int x =, y = ; swap(&x, &y); printf( %d %d\n, x, y); return 0; x: y: 34
Pointers and Function Arguments Change the value of an actual parameter variable scanf demystified char ch; int numx; float numy; scanf( %c %d %f, &ch, &numx, &numy); 35
Pointer arithmetic C allows pointer values to be incremented by integer values char *m =?dog?; d (char) o (char) g (char) NUL (char) char result = *(m + ); (char *) m m gives an address of a char (m + ) gives the char one byte higher *(m + ) instructs us to take the contents of that address result gets the value 'o' o (char) result 36
Pointer arithmetic A slightly more complex example: char *m =?dog?; d (char) o (char) g (char) NUL (char) char result = *++m; m gives an address of a char ++m changes m, to the address one byte higher, and returns the new address *++m instructs us to take the contents of that location result gets the value 'o' (char *) m o (char) result 37
Pointer arithmetic How about multibyte values? Q: Each char value occupies exactly one byte, so obviously incrementing the pointer by one takes you to a new char value... But what about types like int that span more than one byte? A: C does the right thing : increments the pointer by the size of one int value 7 (int) 4 (int) int a[] = 7, 4; int *m = a; int result = *++m; (int *) m 4 (char) (int) result 38
Example: strcpy string copy char *strcpy(char *dest, const char *src) const char *p; char *q; for(p = src, q = dest; *p!= '\0'; p++, q++) *q = *p; *q = '\0'; return dest; (char *) src d (char) o (char) g (char) NUL (char) p (char *) (char *) q 39 (char *) dest d o (char) (char) (char) (char) g NUL