Presentation is loading. Please wait.

Presentation is loading. Please wait.

Plab 2003 Exercise 4. Pointers to pointers. Plab 2003 Exercise 4 2 Pointers to pointers (1)int i=3 (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: (4)int.

Similar presentations


Presentation on theme: "Plab 2003 Exercise 4. Pointers to pointers. Plab 2003 Exercise 4 2 Pointers to pointers (1)int i=3 (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: (4)int."— Presentation transcript:

1 Plab 2003 Exercise 4. Pointers to pointers

2 Plab 2003 Exercise 4 2 Pointers to pointers (1)int i=3 (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: (4)int *ip1 = &i; (5)int *ip2 = &j; ipp: (6)int **ipp = &ip1;

3 Plab 2003 Exercise 4 3 Pointers to pointers (1)int i=3; (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: ipp: (4)int *ip1 = &i; (5)int *ip2 = &j; (6)int **ipp = &ip1; (7)ipp = &ip2;

4 Plab 2003 Exercise 4 4 Pointers to pointers (1)int i=3; (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: ipp: (4)int *ip1 = &i; (5)int *ip2 = &j; (6)int **ipp = &ip1; (7)ipp = &ip2; (8)*ipp = &k;

5 Plab 2003 Exercise 4 5 pointers to pointers: example //put pointer to an allocated string in pp (1)int allocString(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 “allocString” (8)void main() { (9)char *s = “example”; (10)char *copy = NULL; (11)allocString( strlen(s), &copy ); (12)strcpy( copy, string); (13)}

6 Plab 2003 Exercise 4 6 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 func( int x[5][7] ) //ok  void func( int x[][7] ) //ok  void func( int x[][] ) //error (always)  void func( int * x[] ) //error  void func( int ** x ) //error

7 Plab 2003 Exercise 4 7 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 }

8 Plab 2003 Exercise 4 8 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)}

9 Plab 2003 Exercise 4 9 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 );

10 Plab 2003 Exercise 4 10 pointers to pointers to … We also have pointers to pointers to pointers, etc. double ** mat1 = getMatrix(); double ** mat2 = getMatrix(); // allocate an array of matrices double *** matrices = (double***) malloc( n*sizeof( double**) ); matrices[0] = mat1; matrices[1] = mat2;

11 Plab 2003 Exercise 4. Test your understanding

12 Plab 2003 Exercise 4 12 Pointers void func( int * ptr ) { ptr++; ptr += 1; } int main () { int arr[] = {0,0}; int * p = arr; func(p); p++; *p += 1; printf("%d %d\n",arr[1],arr[0]); }

13 Plab 2003 Exercise 4 13 Pointers Cont (1) int i; (2) int * const px = &i; (3) int j = 5; (4) px = &j; (5) px[1] = j; 1. Line 2 will generate compilation errors/warnings 2. Line 4 will generate compilation errors/warnings 3. Line 5 will generate compilation errors/warnings 4. The code will not generate compilation errors/warnings

14 Plab 2003 Exercise 4 14 Macro #define MAX(a,b) \ (((a) > (b)) ? (a) : (b) ) int i=2; int j=3; int x = MAX(i++,j); printf("%d %d\n",i,x);

15 Plab 2003 Exercise 4 15 Macro #define BAR(x) x ) #define FOO(x) ( x++ int a = 2; int b = 3; int c = FOO(a) + BAR(b); 1. 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 error.

16 Plab 2003 Exercise 4 16 Memory Management #include 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]); }

17 Plab 2003 Exercise 4 17 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?

18 Plab 2003 Exercise 4 18 Strings struct Flight { char source[20]; char * destination; } ticket1, ticket2; ticket1.destination = (char*)malloc( 20*sizeof(char) ); strcpy(ticket1.source, "Florence"); strcpy(ticket1.destination,“London"); ticket2 = ticket1; *(ticket2.destination + 2) = 'k'; ticket2.source[1] = 'm'; printf("%s %s",ticket1.source,ticket2.destination);

19 Plab 2003 Exercise 4 19 Strings Cont char s[] = {'p','l','a','b'}; What will be returned by calling strcmp(s,"plab")? 1. a negative number 2. a positive number 3. 0 4. We cannot know for sure

20 Plab 2003 Exercise 4 20 Strings Cont (1) char s[] = "plab"; (2) (2) (*s)++; (3) (3) *(s+2) = '\0'; 1. line 2 will generate a compilation error 2. line 3 will generate a compilation error 3. after runnung this code the string s is "ql" 4. after runnung this code the string s is "pl" 5. after running this code the string s is "plab"

21 Plab 2003 Exercise 4 21 Union Suppose that sizeof(char) = 1, sizeof(int) = 4, sizeof(double)=8; union ID { char x; double y; }; sizeof( ID ) is ?


Download ppt "Plab 2003 Exercise 4. Pointers to pointers. Plab 2003 Exercise 4 2 Pointers to pointers (1)int i=3 (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: (4)int."

Similar presentations


Ads by Google