Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Pointer Tran, Van Hoai

2 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 p c Memory cells PointerVariable

3 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;

4 Variable in memory

5 Pointer, Variable in memory

6 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

7 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 */

8 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 */

9 Function arguments without pointer? WWRONG After swap(a,b), a is still 1, b still 2 C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 ); …

10 Why it does not work?  No way to return new values to a and b b 1 2 a caller 1 2 x y swap() (before actions) passing values swap() (after actions) 2 1 x y swapping values

11 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 ); …

12 Why it works?  px points to a  Accessing *px means accessing a a b caller px py swap()

13 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; }

14 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 */ 23 a a[0]a[2] 2 xp a[4]

15 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

16 Pointer to Pointer (semantic meaning) (1)  Challenge How to pass an array of int s to a function The function inserts an int into the array /* 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; }

17 Pointer to Pointer (semantic meaning) (2)  The function increase the size of the array if needed /* 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; }

18 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 …

19 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)( …, …, … )

20 Where to use (for example)?  Look into example4.c

21 Confusion  int *f() function returning a pointer to int  int (*f)() pointer to function returning int

22 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)()

23 Memory organization Tran, Van Hoai

24 Three types  Automatic storage (stack)  Static storage  Free storage (heap)

25 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)

26 Static  Used for Global, static (in functions) objects  Created once, existing during program execution  Default value: binary zero

27 Heap (dynamic)  Used for dynamic memory allocation  calloc(), malloc(), free()  Created, Destroyed explicitly in user C code  Default value: unspecified


Download ppt "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."

Similar presentations


Ads by Google