Upcoming SlideShare
×

# 9. pointer, pointer & function

793 views

Published on

Published in: Technology, Lifestyle
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
793
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
26
0
Likes
1
Embeds 0
No embeds

No notes for slide

### 9. pointer, pointer & function

1. 1. Pointer
2. 2. 2Pointer Declaration and Assignment Variables and address– Structure of Memory– Each variable is placed in a specified address• Pointer: Address of variables…1000 ‘a’10013.210054…ch, 1bytesf, 4bytesi, 4byteschar ch = ‘a’ ;float f = 3.2 ;int i = 4 ;address
3. 3. 3Pointer Declaration and Assignment Variables and address– We can get pointers of variables from the variableschar ch = ‘a’ ;float f = 3.2 ;int i = 4 ;printf( “%d %d %dn”, &a, &f, &i ) ;&(name of variable) == address of variable …1000 ‘a’10013.210054…Address ofoperator
4. 4. 4Pointer Declaration and Assignment Variables and address– We can access the variable with the pointer of the variablechar ch ;float f ;int i ;*(&ch) = ‘a’ ;*(&f) = 3.2 ;*(&i) = 4 ;*(address) means the variable in the address…1000 ‘a’10013.210054…char ch ;float f ;int i ;ch = ‘a’ ;f = 3.2 ;i = 4 ;Indirectoperator
5. 5. 5Pointer Declaration and Assignment Pointer Variable– The variable which can hold addresses of variableschar 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 holdonly ‘char’ type pointerVariable can holdonly ‘float’ type pointerVariable can holdonly ‘int’ type pointerchar *pch = &ch ;float *pf = &f ;int *pi = &i ;
6. 6. 6Pointer Declaration and Assignment Pointer Variablechar 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’10013.210054…
7. 7. 7Pointer 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. 8. 8Pointer Declaration and Assignment Pointer Variable– Since a pointer variable is also a variable, it has it ownaddress, like other variables doint i = 0 ;int *pi = &i;printf( “%dn”, i ) ;printf( “%dn”, *pi ) ;printf( “%dn”, &pi ) ;1000…1020…pi, 4 bytesi, 4 bytes
9. 9.  Example9Pointer Declaration and Assignment[Ex] int *p;int month=3;p = &month;Assign the address of ‘month’to pointer variablep month3p1000month31000Use an arrow instead ofwriting address in apointer variable
10. 10.  Example10Addressing and Dereferencing[Ex] int a, b;int *p;a = b = 7;p = &a;printf(“*p = %dn”, *p);*p = 3;printf(“a = %dn”, a);*p == 7The variable pointed by p, that is aThe variable pointed by p, a, has 3assign the address of a to p*p = 7a = 3
11. 11. 11Addressing and Dereferencing[Ex1] int a, b;int *p;a = b = 7;p = &a;*p = 3;p = &b;*p = 2 * *p – a;pa7b7p3a b7pa3 11b Example
12. 12.  Prone to error12Addressing 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 pbecause p is not initialized.We don’t know where p pointsError!!Can’t assign value x to pointer pbecause p cannot hold an integer variable
13. 13. 13Multi 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 ofinteger variables• The size of pointer variable q is 4 byte (32bit machine)int i = 4 ;int *p ;int **q ;
14. 14.  Example14Pointer Declaration and Assignment[Ex] int i = 3;int *p ;int **q ;p = &i ;q = &p ;p i3i31000p10002000q20003000q
15. 15.  Example:– What are the values of i and j,respectively?– Assume that the addresses of i,j, p and q are 1000, 2000, 3000and 4000, respectively.– What are the values of p, q, r?15Pointer 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. 16.  Example16Swap 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?
17. 17.  Example17Swap 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 7after swap : 7 3
18. 18.  Swap values of two pointer variables18Swap 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 7after swap : 7 3
19. 19. 19Call-by-Value1 1a in main a in functionmain() function function() functiona = a + 1 ;When function(a); iscalled, value of a in mainis copied to a in function2After Return;1 Variable a in main and a in function arenot identical, so that value of a in mainis not changed1
20. 20. 20Call-by-Value1 1a in main a in functionmain() function function() functiona = a + 1 ;When function(a); iscalled, value of a in mainis copied to a in function2After Return;1 Variable a in main and a in function arenot identical, so that value of a in mainis not changed1