Pointer
description
Transcript of Pointer
![Page 1: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/1.jpg)
PointerTran, Van Hoai
![Page 2: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/2.jpg)
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
Pointer Variable
![Page 3: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/3.jpg)
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 pp = &c;
![Page 4: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/4.jpg)
Variable in memory
![Page 5: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/5.jpg)
Pointer, Variable in memory
![Page 6: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/6.jpg)
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
![Page 7: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/7.jpg)
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 */
![Page 8: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/8.jpg)
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 */
![Page 9: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/9.jpg)
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 );…
![Page 10: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/10.jpg)
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
![Page 11: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/11.jpg)
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 );…
![Page 12: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/12.jpg)
Why it works? px points to a Accessing *px means accessing a
a
b
caller
px
py
swap()
![Page 13: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/13.jpg)
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;}
![Page 14: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/14.jpg)
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 */
2 3a
a[0] a[2]
2x p
a[4]
![Page 15: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/15.jpg)
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
![Page 16: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/16.jpg)
Pointer to Pointer (semantic meaning) (1) Challenge
How to pass an array of ints 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;}
![Page 17: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/17.jpg)
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;}
![Page 18: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/18.jpg)
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 …
![Page 19: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/19.jpg)
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); AssignmentpLeast = intLeast; Usage(*pLeast)( …, …, … )
![Page 20: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/20.jpg)
Where to use (for example)? Look into example4.c
![Page 21: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/21.jpg)
Confusion int *f()
function returning a pointer to int int (*f)()
pointer to function returning int
![Page 22: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/22.jpg)
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)()
![Page 23: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/23.jpg)
Memory organizationTran, Van Hoai
![Page 24: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/24.jpg)
Three types Automatic storage (stack) Static storage Free storage (heap)
![Page 25: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/25.jpg)
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)
![Page 26: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/26.jpg)
Static Used for
Global, static (in functions) objects Created once, existing during program
execution Default value: binary zero
![Page 27: Pointer](https://reader036.fdocuments.in/reader036/viewer/2022062810/56815d8d550346895dcb9bbf/html5/thumbnails/27.jpg)
Heap (dynamic) Used for
dynamic memory allocation calloc(), malloc(), free()
Created, Destroyed explicitly in user C code
Default value: unspecified