270 likes | 451 Views
Pointer. Tran, Van Hoai. Pointers and Addresses. Pointer: group of cells ( 2,4 cells ) Variable: group of cells Pointer is also a variable Each cell (or group of cells) has an address. Pointer. Variable. p. c. Memory cells. Pointers and Addresses.
E N D
Pointer Tran, Van Hoai
Pointers and Addresses • Pointer: group of cells (2,4 cells) • Variable: group of cells • Pointer is also a variable • Each cell (or group of cells) has an address Pointer Variable p c Memory cells
Pointers and Addresses • &: get address of an object in memory • The address of a pointer &p p c Memory cells // assign the address of c to the variable p p = &c;
Dereference • How to access the object the pointer points to? • *: derefererencing operator • *p and c access the same object • If p points to c, *p can occur in any context where c could do p c
Example • int *ip; • intended as a mnemonic (dễ nhớ) • implying *ip is an int /* how to declare */ int x=1, y=2; int *ip; /* ip is pointer to int */ /* how to use */ ip = &x; /* ip points to x */ y = *ip; /* y is now valued to that */ /* of x, i.e., 1 */ *ip = 0; /* x is now 0 */
More examples /* how to declare */ int x=1, y=2; int *ip; /* ip is pointer to int */ int **ipp; /* ipp is a pointer to int* */ /* how to use */ ip = &x; /* ip points to x */ *ip += 1; /* x is now 2 */ ipp = &x;/* invalid */ ipp = &ip; /* ipp points to ip */ **ipp = 5; /* x is now 5 */ *ipp = 2; /* invalid */ *ipp = &y; /* ip points to y */ **ipp = 3; /* y is now 3 */
Function arguments without pointer? • WRONG • After swap(a,b), a is still 1, b still 2 • C passes arguments to functions by value • only swap copies of a and b /* callee */ void swap( int x, int y ) { int temp; temp = x; x = y; y = temp; } /* caller */ int a=1, b=2; … swap( a, b ); …
caller 1 a 2 b passing values swap() (before actions) swap() (after actions) 1 2 x x swapping values 2 1 y y Why it does not work? • No way to return new values to a and b
How to swap values of a and b? • Pointers passed to function • Actions performed indirectly on variables of caller /* callee */ void swap( int *px, int *py ) { int temp; temp = *px; *px = *py; *py = temp; } /* caller */ int a=1, b=2; … swap( &a, &b ); …
Why it works? • px points to a • Accessing *px means accessing a caller a b swap() px py
Another way to return results to caller? • Through return mechanism • How to return more than 1 outputs? • arguments /* n! */ int factorial( int n ) { int i, f=1; for( i=1; i<=n; i++ ) f *= i; return f; } int factorial( int *fac, int n ) { int i; *fac = 1; if ( n<0 ) return 0; for( i=1; i<=n; i++ ) *fac *= i; return 1; }
Array and Pointer int x=2; int *p; /* a pointer to int */ int a[5]; /* array of 5 ints */ a[2]=x; /* a[2] is 2 */ p=&a[2]; /* p points to 3rd element of a */ *(p+2)=3; /* a[4] is 3 now */ p=a; /* p points to a[0] */ a=p;/* invalid */ a 2 3 a[0] a[2] a[4] x 2 p
Pointer to Pointer(syntactic meaning) • What is the meaning of void myFunction( int **ipp ) { … } • Think syntactically in step • ipp points to int* which points to int
Pointer to Pointer (semantic meaning) (1) /* ip: pointer to int */ /* n: length of array */ /* elm: element to be inserted */ int insert( int *ip, int n, int elm ) { int i, j; for( i=0; i<n; i++ ) if ( ip[i] > elm ) /* insert position here */ break; if ( i<n ) /* move the rest forward */ for( j=n-1; j>=i; j-- ) /* 1 unit if needed */ ip[j+1] = ip[j]; ip[i] = elm; /* safe to insert now */ return n+1; } • Challenge • How to pass an array of ints to a function • The function inserts an int into the array
Pointer to Pointer(semantic meaning) (2) /* ipp: pointer to pointer */ /* s: size of array */ int insert( int **ipp, int *n, int *s, int elm ) { int i, j, *p; if ( n+1 > s ){ /* re-allocate mem. if needed */ p = (int*)calloc( n+1, sizeof(int) ); memcpy( p, *ipp, n*sizeof(int) ); free( *ipp ); *ipp = p; } for( i=0; i<n; i++ ) /* find position to insert */ if ( p[i] > elm ) break; if ( i<n ) /* move the rest forward */ for( j=n-1; j>=i; j-- ) /* 1 unit if needed */ p[j+1] = p[j]; p[i] = elm; /* safe to insert now */ n = n+1; s = s+1; /* update new length and size */ return n+1; } • The function increase the size of the array if needed
Pointer to Function • In C, function is not a variable • But pointer to function is possible • To be assigned • To be placed in arrays • To be passed to functions • To be returned by functions • …
How to declare • There are similar functions • int intLeast(void *p,int n,void *e) • int doubleLeast(void *p,int n,void *e) • Pointer variable to functions • int (*pLeast)(void *,int,void *e); • Assignment pLeast = intLeast; • Usage (*pLeast)( …, …, … )
Where to use (for example)? • Look into example4.c
Confusion • int *f() • function returning a pointer to int • int (*f)() • pointer to function returning int
Examples /* argv: pointer to string */ char **argv /* daytab: pointer to array[13] of int */ int (*daytab)[13] /* daytab: array[13] of pointers to int */ int *daytab[13] /* comp: function returning pointer to void */ void *comp() /* comp: function to pointer returning void */ void (*comp)()
Memory organization Tran, Van Hoai
Three types • Automatic storage (stack) • Static storage • Free storage (heap)
Stack • Used for • Local objects • not explicitly declared static or extern • declared auto or register • function arguments • Created automatically on stack when entering, Destroyed when exiting a block or function • Default value: indeterminate (không xác định)
Static • Used for • Global, static (in functions) objects • Created once, existing during program execution • Default value: binary zero
Heap (dynamic) • Used for • dynamic memory allocation • calloc(),malloc(), free() • Created, Destroyed explicitly in user C code • Default value: unspecified