• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
9. pointer, pointer & function
 

9. pointer, pointer & function

on

  • 442 views

 

Statistics

Views

Total Views
442
Views on SlideShare
297
Embed Views
145

Actions

Likes
0
Downloads
8
Comments
0

1 Embed 145

http://class.skku.goorm.io 145

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    9. pointer, pointer & function 9. pointer, pointer & function Presentation Transcript

    • Pointer
    • 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
    • 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
    • 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
    • 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 ;
    • 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…
    • 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 ) ;
    • 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
    •  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
    •  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
    • 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
    •  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
    • 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 ;
    •  Example14Pointer Declaration and Assignment[Ex] int i = 3;int *p ;int **q ;p = &i ;q = &p ;p i3i31000p10002000q20003000q
    •  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 ;
    •  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?
    •  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
    •  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
    • 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
    • 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