Pointers to pointers Plab 2003 Exercise 4 Pointers
Pointers to pointers Plab 2003 Exercise 4.
Pointers to pointers (1) int i=3 (2) int j=4; (3) int k=5; i: 3 (4) int *ip 1 = &i; (5) int *ip 2 = &j; j: 4 ip 1: k: 5 ip 2: (6) int **ipp = &ip 1; ipp: Plab 2003 Exercise 4 2
Pointers to pointers (1) int i=3; (2) int j=4; (3) int k=5; i: 3 (4) int *ip 1 = &i; (5) int *ip 2 = &j; j: 4 ip 1: k: 5 ip 2: (6) int **ipp = &ip 1; (7) ipp = &ip 2; Plab 2003 Exercise 4 ipp: 3
Pointers to pointers (1) int i=3; (2) int j=4; (3) int k=5; i: 3 (4) int *ip 1 = &i; (5) int *ip 2 = &j; j: 4 ip 1: k: 5 ip 2: (6) int **ipp = &ip 1; (7) ipp = &ip 2; ipp: (8) *ipp = &k; Plab 2003 Exercise 4 4
pointers to pointers: example //put pointer to an allocated string in pp (1) int alloc. String(int len, char ** pp) { (2) char *str = malloc(len + 1); (3) if (str==NULL) (4) return -1; (5) *pp = str; (6) return 0; (7) } // copy a string using “alloc. String” (8) void main() { (9) char *s = “example”; (10) char *copy = NULL; (11) alloc. String( strlen(s), © ); (12) strcpy( copy, string); (13) } Plab 2003 Exercise 4 5
Multi-dimensional arrays Can be created in few ways: 1. Automatically: u int arr[5][7]; · 5 rows, 7 columns · continuous memory (divided to 5 blocks) · access: arr[row][col] = 0; u When sending ‘arr’ as an argument to a function, only the 1 st index can be omitted: · · · void void Plab 2003 Exercise 4 func( func( int int int x[5][7] ) x[][] ) * x[] ) ** x ) //ok //error (always) //error 6
Multi-dimensional arrays 2. Semi-dynamic: u Define an array of pointers: int *pa[5]; // allocates memory for 5 // pointers. for (i=0; i<5; i++) { pa[i] = (int*) malloc( 7*sizeof(int) ); // pa[i] now points to a memory for 7 // ints } Plab 2003 Exercise 4 7
Multi-dimensional arrays 3. Dynamically: (1) int ** array; (2) array = (int**) malloc( 5*sizeof(int*) ); (3) for (i=0; i<5; i++) { (4) arr[i] = malloc( 7*sizeof(int) ); (5) } Plab 2003 Exercise 4 8
Multi-dimensional arrays Dynamically allocated multi-dimensional array: u Memory not continuous u Each pointer can be with different size u Access: arr[ i ][ j ] u Don’t forget to free all the memory for (i=0; i<nrows; i++ ) { free( array[i] ); array[i] = NULL; } free( array ); Plab 2003 Exercise 4 9
pointers to … We also have pointers to pointers, etc. double ** mat 1 = get. Matrix(); double ** mat 2 = get. Matrix(); //allocate an array of matrices double *** matrices = (double***) malloc( n*sizeof( double**) ); matrices[0] = mat 1; matrices[1] = mat 2; Plab 2003 Exercise 4 10
Test your understanding Plab 2003 Exercise 4.
Pointers void func( int * ptr ) { ptr++; ptr += 1; } int main () { int arr[] = {0, 0}; int * p = arr; func(p); p++; *p += 1; printf("%d %dn", arr[1], arr[0]); } Plab 2003 Exercise 4 12
Pointers Cont (1) (2) (3) (4) (5) 1. 2. 3. 4. int i; int * const px = &i; int j = 5; px = &j; px[1] = j; Line 2 will generate compilation errors/warnings Line 4 will generate compilation errors/warnings Line 5 will generate compilation errors/warnings The code will not generate compilation errors/warnings Plab 2003 Exercise 4 13
Macro #define MAX(a, b) (((a) > (b)) ? (a) : (b) ) int i=2; int j=3; int x = MAX(i++, j); printf("%d %dn", i, x); Plab 2003 Exercise 4 14
Macro #define int a = int b = int c = BAR(x) x ) FOO(x) ( x++ 2; 3; FOO(a) + BAR(b); This code will generate a compilation error. 2. This code will generate a run-time error. 3. The code will compile without problems. 4. This code will generate a pre-processing Plab 2003 error. Exercise 4 1. 15
Memory Management #include <stdio. h> void f(int* pa[]) { for (int i=0; i<5; i++) pa[i] = (int*)malloc(7*sizeof(int)); } int main() { int* pa[5]; for( int i=0; i<5; i++ ) pa[i] = (int*)malloc(7*sizeof(int)); f(pa); for (int i=0; i<5; i++) free(pa[i]); } Plab 2003 Exercise 4 16
Memory Management Cont int main() { (1) int *y = (int*)malloc( 3*sizeof(int) ); (2) int x[] = {1, 2, 3}; (3) int ** pp = NULL; (4) int *z = y; } In which of the following lines a space was allocated on the memory heap? Plab 2003 Exercise 4 17
Strings struct Flight { char source[20]; char * destination; } ticket 1, ticket 2; ticket 1. destination = (char*)malloc( 20*sizeof(char) ); strcpy(ticket 1. source, "Florence"); strcpy(ticket 1. destination, “London"); ticket 2 = ticket 1; *(ticket 2. destination + 2) = 'k'; ticket 2. source[1] = 'm'; printf("%s %s", ticket 1. source, ticket 2. destination); Plab 2003 Exercise 4 18
Strings Cont char s[] = {'p', 'l', 'a', 'b'}; What will be returned by calling strcmp(s, "plab")? 1. 2. 3. 4. a negative number a positive number 0 We cannot know for sure Plab 2003 Exercise 4 19