KEMBAR78
9. pointer, pointer & function | PDF
Pointer
2
Pointer Declaration and Assignment
 Variables and address
– Structure of Memory
– Each variable is placed in a specified address
• Pointer: Address of variables
…
1000 ‘a’
1001
3.2
1005
4
…
ch, 1bytes
f, 4bytes
i, 4bytes
char ch = ‘a’ ;
float f = 3.2 ;
int i = 4 ;
address
3
Pointer Declaration and Assignment
 Variables and address
– We can get pointers of variables from the variables
char ch = ‘a’ ;
float f = 3.2 ;
int i = 4 ;
printf( “%d %d %dn”, &a, &f, &i ) ;
&(name of variable) == address of variable …
1000 ‘a’
1001
3.2
1005
4
…
Address of
operator
4
Pointer Declaration and Assignment
 Variables and address
– We can access the variable with the pointer of the variable
char ch ;
float f ;
int i ;
*(&ch) = ‘a’ ;
*(&f) = 3.2 ;
*(&i) = 4 ;
*(address) means the variable in the address
…
1000 ‘a’
1001
3.2
1005
4
…
char ch ;
float f ;
int i ;
ch = ‘a’ ;
f = 3.2 ;
i = 4 ;
Indirect
operator
5
Pointer Declaration and Assignment
 Pointer Variable
– The variable which can hold addresses of variables
char ch = ‘a’ ;
float f = 3.2 ;
int i = 4 ;
char *pch ;
float *pf ;
int *pi ;
pch = &ch ;
pf = &f ;
pi = &i ;
printf( “%d %d %dn”, pch, pf, pi ) ;
data_type * pointer_varaible;
Variable can hold
only ‘char’ type pointer
Variable can hold
only ‘float’ type pointer
Variable can hold
only ‘int’ type pointer
char *pch = &ch ;
float *pf = &f ;
int *pi = &i ;
6
Pointer Declaration and Assignment
 Pointer Variable
char ch ;
float f ;
int i ;
ch = ‘a’ ;
f = 3.2 ;
i = 4 ;
char ch ;
float f ;
int i ;
char *pch = &ch ;
float *pf = &f ;
int *pi = &i ;
*pch = ‘a’ ;
*pf = 3.2 ;
*pi = 4 ;
…
1000 ‘a’
1001
3.2
1005
4
…
7
Pointer Declaration and Assignment
 Pointer Variable
– The size of all pointer variables is 4 bytes (4 byte machine)
• why??
char *pch ;
float *pf ;
int *pi ;
printf( “%dn”, sizeof(pch) ) ;
printf( “%dn”, sizeof(pf) ) ;
printf( “%dn”, sizeof(pi) ) ;
int i = 0 ;
int *pi = & i;
printf( “%dn”, i ) ;
printf( “%dn”, *pi ) ;
printf( “%dn”, &pi ) ;
8
Pointer Declaration and Assignment
 Pointer Variable
– Since a pointer variable is also a variable, it has it own
address, like other variables do
int i = 0 ;
int *pi = &i;
printf( “%dn”, i ) ;
printf( “%dn”, *pi ) ;
printf( “%dn”, &pi ) ;
1000
…
1020
…
pi, 4 bytes
i, 4 bytes
 Example
9
Pointer Declaration and Assignment
[Ex] int *p;
int month=3;
p = &month;
Assign the address of ‘month’
to pointer variable
p month
3
p
1000
month
31000
Use an arrow instead of
writing address in a
pointer variable
 Example
10
Addressing and Dereferencing
[Ex] int a, b;
int *p;
a = b = 7;
p = &a;
printf(“*p = %dn”, *p);
*p = 3;
printf(“a = %dn”, a);
*p == 7
The variable pointed by p, that is a
The variable pointed by p, a, has 3
assign the address of a to p
*p = 7
a = 3
11
Addressing and Dereferencing
[Ex1] int a, b;
int *p;
a = b = 7;
p = &a;
*p = 3;
p = &b;
*p = 2 * *p – a;
pa
7
b
7
p
3
a b
7
pa
3 11
b
 Example
 Prone to error
12
Addressing and Dereferencing
[Ex1] int x, *p;
x = 10;
*p = x;
[Ex3] int x, *p;
x = 10;
p = x;
Error!!
Can’t assign value x to pointer p
because p is not initialized.
We don’t know where p points
Error!!
Can’t assign value x to pointer p
because p cannot hold an integer variable
13
Multi Pointer Variable
 Multi Pointer Variable
– i is a integer variable
– p is a pointer can hold the address of an integer variable
– q is a pointer can hold the address of a pointer variable of
integer variables
• The size of pointer variable q is 4 byte (32bit machine)
int i = 4 ;
int *p ;
int **q ;
 Example
14
Pointer Declaration and Assignment
[Ex] int i = 3;
int *p ;
int **q ;
p = &i ;
q = &p ;
p i
3
i
31000
p
10002000
q
20003000
q
 Example:
– What are the values of i and j,
respectively?
– Assume that the addresses of i,
j, p and q are 1000, 2000, 3000
and 4000, respectively.
– What are the values of p, q, r?
15
Pointer Declaration and Assignment
[Ex] int i = 3, j = 2;
int *p, *q ;
int **r ;
p = &i ;
q = &j ;
r = &p ;
*p = 4 ;
*q = 5 ;
**r = 6 ;
*r = &q ;
q =&i ;
**r = 7 ;
 Example
16
Swap Function
[Ex]
void swap(int i, int j) {
int temp = i ;
i = j;
j = temp;
}
int main(void) {
int a=3, b=7;
printf(“before swap : %d %dn”, a, b);
swap( a, b );
printf(“after swap : %d %dn”, a, b);
}
Can it swap values of a and b?
 Example
17
Swap Function
[Ex]
void swap(int *p, int *q) {
int temp = *p ;
*p = *q;
*q = temp;
}
int main(void) {
int a=3, b=7;
printf(“before swap : %d %dn”, a, b);
swap(&a, &b);
printf(“after swap : %d %dn”, a, b);
}
before swap : 3 7
after swap : 7 3
 Swap values of two pointer variables
18
Swap Function
[Ex]
void swap(int **pp, int **qq) {
int *temp = *pp ;
*pp = *qq;
*qq = temp;
}
int main(void) {
int a=3, b=7;
int *p = &a, *q = &b ;
printf(“before swap : %d %dn”, *p, *q);
swap(&p, &q);
printf(“after swap : %d %dn”, *p, *q);
}
before swap : 3 7
after swap : 7 3
19
Call-by-Value
1 1
a in main a in function
main() function function() function
a = a + 1 ;
When function(a); is
called, value of a in main
is copied to a in function
2
After Return;
1 Variable a in main and a in function are
not identical, so that value of a in main
is not changed
1
20
Call-by-Value
1 1
a in main a in function
main() function function() function
a = a + 1 ;
When function(a); is
called, value of a in main
is copied to a in function
2
After Return;
1 Variable a in main and a in function are
not identical, so that value of a in main
is not changed
1

9. pointer, pointer & function

  • 1.
  • 2.
    2 Pointer Declaration andAssignment  Variables and address – Structure of Memory – Each variable is placed in a specified address • Pointer: Address of variables … 1000 ‘a’ 1001 3.2 1005 4 … ch, 1bytes f, 4bytes i, 4bytes char ch = ‘a’ ; float f = 3.2 ; int i = 4 ; address
  • 3.
    3 Pointer Declaration andAssignment  Variables and address – We can get pointers of variables from the variables char ch = ‘a’ ; float f = 3.2 ; int i = 4 ; printf( “%d %d %dn”, &a, &f, &i ) ; &(name of variable) == address of variable … 1000 ‘a’ 1001 3.2 1005 4 … Address of operator
  • 4.
    4 Pointer Declaration andAssignment  Variables and address – We can access the variable with the pointer of the variable char ch ; float f ; int i ; *(&ch) = ‘a’ ; *(&f) = 3.2 ; *(&i) = 4 ; *(address) means the variable in the address … 1000 ‘a’ 1001 3.2 1005 4 … char ch ; float f ; int i ; ch = ‘a’ ; f = 3.2 ; i = 4 ; Indirect operator
  • 5.
    5 Pointer Declaration andAssignment  Pointer Variable – The variable which can hold addresses of variables char ch = ‘a’ ; float f = 3.2 ; int i = 4 ; char *pch ; float *pf ; int *pi ; pch = &ch ; pf = &f ; pi = &i ; printf( “%d %d %dn”, pch, pf, pi ) ; data_type * pointer_varaible; Variable can hold only ‘char’ type pointer Variable can hold only ‘float’ type pointer Variable can hold only ‘int’ type pointer char *pch = &ch ; float *pf = &f ; int *pi = &i ;
  • 6.
    6 Pointer Declaration andAssignment  Pointer Variable char ch ; float f ; int i ; ch = ‘a’ ; f = 3.2 ; i = 4 ; char ch ; float f ; int i ; char *pch = &ch ; float *pf = &f ; int *pi = &i ; *pch = ‘a’ ; *pf = 3.2 ; *pi = 4 ; … 1000 ‘a’ 1001 3.2 1005 4 …
  • 7.
    7 Pointer Declaration andAssignment  Pointer Variable – The size of all pointer variables is 4 bytes (4 byte machine) • why?? char *pch ; float *pf ; int *pi ; printf( “%dn”, sizeof(pch) ) ; printf( “%dn”, sizeof(pf) ) ; printf( “%dn”, sizeof(pi) ) ; int i = 0 ; int *pi = & i; printf( “%dn”, i ) ; printf( “%dn”, *pi ) ; printf( “%dn”, &pi ) ;
  • 8.
    8 Pointer Declaration andAssignment  Pointer Variable – Since a pointer variable is also a variable, it has it own address, like other variables do int i = 0 ; int *pi = &i; printf( “%dn”, i ) ; printf( “%dn”, *pi ) ; printf( “%dn”, &pi ) ; 1000 … 1020 … pi, 4 bytes i, 4 bytes
  • 9.
     Example 9 Pointer Declarationand Assignment [Ex] int *p; int month=3; p = &month; Assign the address of ‘month’ to pointer variable p month 3 p 1000 month 31000 Use an arrow instead of writing address in a pointer variable
  • 10.
     Example 10 Addressing andDereferencing [Ex] int a, b; int *p; a = b = 7; p = &a; printf(“*p = %dn”, *p); *p = 3; printf(“a = %dn”, a); *p == 7 The variable pointed by p, that is a The variable pointed by p, a, has 3 assign the address of a to p *p = 7 a = 3
  • 11.
    11 Addressing and Dereferencing [Ex1]int a, b; int *p; a = b = 7; p = &a; *p = 3; p = &b; *p = 2 * *p – a; pa 7 b 7 p 3 a b 7 pa 3 11 b  Example
  • 12.
     Prone toerror 12 Addressing and Dereferencing [Ex1] int x, *p; x = 10; *p = x; [Ex3] int x, *p; x = 10; p = x; Error!! Can’t assign value x to pointer p because p is not initialized. We don’t know where p points Error!! Can’t assign value x to pointer p because p cannot hold an integer variable
  • 13.
    13 Multi Pointer Variable Multi Pointer Variable – i is a integer variable – p is a pointer can hold the address of an integer variable – q is a pointer can hold the address of a pointer variable of integer variables • The size of pointer variable q is 4 byte (32bit machine) int i = 4 ; int *p ; int **q ;
  • 14.
     Example 14 Pointer Declarationand Assignment [Ex] int i = 3; int *p ; int **q ; p = &i ; q = &p ; p i 3 i 31000 p 10002000 q 20003000 q
  • 15.
     Example: – Whatare the values of i and j, respectively? – Assume that the addresses of i, j, p and q are 1000, 2000, 3000 and 4000, respectively. – What are the values of p, q, r? 15 Pointer Declaration and Assignment [Ex] int i = 3, j = 2; int *p, *q ; int **r ; p = &i ; q = &j ; r = &p ; *p = 4 ; *q = 5 ; **r = 6 ; *r = &q ; q =&i ; **r = 7 ;
  • 16.
     Example 16 Swap Function [Ex] voidswap(int i, int j) { int temp = i ; i = j; j = temp; } int main(void) { int a=3, b=7; printf(“before swap : %d %dn”, a, b); swap( a, b ); printf(“after swap : %d %dn”, a, b); } Can it swap values of a and b?
  • 17.
     Example 17 Swap Function [Ex] voidswap(int *p, int *q) { int temp = *p ; *p = *q; *q = temp; } int main(void) { int a=3, b=7; printf(“before swap : %d %dn”, a, b); swap(&a, &b); printf(“after swap : %d %dn”, a, b); } before swap : 3 7 after swap : 7 3
  • 18.
     Swap valuesof two pointer variables 18 Swap Function [Ex] void swap(int **pp, int **qq) { int *temp = *pp ; *pp = *qq; *qq = temp; } int main(void) { int a=3, b=7; int *p = &a, *q = &b ; printf(“before swap : %d %dn”, *p, *q); swap(&p, &q); printf(“after swap : %d %dn”, *p, *q); } before swap : 3 7 after swap : 7 3
  • 19.
    19 Call-by-Value 1 1 a inmain a in function main() function function() function a = a + 1 ; When function(a); is called, value of a in main is copied to a in function 2 After Return; 1 Variable a in main and a in function are not identical, so that value of a in main is not changed 1
  • 20.
    20 Call-by-Value 1 1 a inmain a in function main() function function() function a = a + 1 ; When function(a); is called, value of a in main is copied to a in function 2 After Return; 1 Variable a in main and a in function are not identical, so that value of a in main is not changed 1