Upload
rahul-kulshrestha
View
212
Download
0
Embed Size (px)
Citation preview
7/29/2019 poindfgrgvcfvffdvter.pdf
1/34
ESC101NFundamentals of Computing
Arnab Bhattacharya
Indian Institute of Technology, Kanpurhttp://www.iitk.ac.in/esc101/
1st
semester, 2010-11
Tue, Wed, Fri 0800-0900 at L7
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 1 / 18
http://localhost/var/www/apps/conversion/tmp/scratch_3/[email protected]://www.iitk.ac.in/esc101/http://www.iitk.ac.in/esc101/http://localhost/var/www/apps/conversion/tmp/scratch_3/[email protected]://find/7/29/2019 poindfgrgvcfvffdvter.pdf
2/34
Sizes of variables
The size of a variable can be found out using the sizeof function
p r i n t f ( % d \ n , s i z e o f ( int ) ) ;
In this machinechar: 1 byteint: 4 bytesfloat: 4 bytes
double: 8 bytesWhy are these required?
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 2 / 18
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
3/34
Sizes of variables
The size of a variable can be found out using the sizeof function
p r i n t f ( % d \ n , s i z e o f ( int ) ) ;
In this machinechar: 1 byteint: 4 bytesfloat: 4 bytes
double: 8 bytesWhy are these required?
To know the limitsType Minimum limit Maximum limit
char -128 127int -2147483648 2147483647
Type Maximum precision Minimum limit Maximum limit
float 1.175494E-38 -3.403823E38 3.403823E38double 2.225074E-308 -1.798693E308 1.798693E308
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 2 / 18
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
4/34
Sizes of variables
# i n c l u d e < s t d i o . h >
in t m a i n ( ){char c ;p r in t f (" S iz e o f c h ar a ct e r = % d b yt es \ n " , sizeof ( c ) ) ;
int i ;p r in t f (" S iz e o f i nt = % d b yt e s \n " , sizeof ( i ) ) ;
float f ;p r in t f (" S iz e o f f lo at = % d b yt es \ n " , sizeof ( f ) ) ;
double d ;p r in t f (" S iz e o f d o ub le = % d b y te s \ n" , sizeof ( d ) ) ;
}
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 3 / 18
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
5/34
Limits of variables
# i n c l u d e < s t d i o . h ># i n c l u d e < l i m i t s . h > / / f or l i mi ts o n i n te g er t yp es
# i n c l u d e < float .h >/ / f or l i m it s o n f l oa t in g - p o i nt t y pe s
void m a i n ( ){
p r in t f (" c ha r s t or es v al u es f ro m % d t o % d \ n" , C HA R_ MI N , C H AR _ MA X ) ;p r i n t f ( " \ n " ) ;p r in t f (" i nt s to r es v a lu e s f ro m % d t o % d \ n" , I NT _M IN , I N T_ M AX ) ;p r i n t f ( " \ n " ) ;p r in t f (" S m al l es t n on - z e r o v al ue o f t yp e f lo at i s % e \ n" , F L T_ M IN ) ;p r in t f (" L a rg es t v al ue o f t yp e f lo at i s % e \ n" , F L T_ MA X ) ;p r in t f (" S m al l es t v al u e o f t yp e f lo at i s % e \ n" , - F L T_ M AX ) ;p r in t f (" S m al l es t n on - z e r o a d di t io n v al ue o f t yp e f lo at i s % e \ n" , F L T_ E PS I LO N ) ;p r i n t f ( " \ n " ) ;p r in t f (" S m al l es t n on - z e r o v al ue o f t yp e d o ub le i s % e \ n" , D B L_ MI N ) ;p r in t f (" L a rg es t v al ue o f t yp e d o ub l e i s % e \ n" , D B L_ M AX ) ;p r in t f (" S m al l es t v al u e o f t yp e d ou b le i s % e \ n" , - D B L_ MA X ) ;p r in t f (" S m al l es t n on - z e r o a d di t io n v al ue o f t yp e d o ub l e i s % e \ n" , D B L_ E PS I LO N ) ;p r i n t f ( " \ n " ) ;
}
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 4 / 18
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
6/34
Memory model
Memory is a list of bytes, each having a particular index called address
c
10
i
7 ...4
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 5 / 18
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
7/34
Memory model
Memory is a list of bytes, each having a particular index called address
c
10
i
7 ...4
All variables are stored in memory
Depending on the size, contiguous bytes are occupied
The address of a variable is the first byte where it is stored
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 5 / 18
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
8/34
Memory model
Memory is a list of bytes, each having a particular index called address
c
10
i
7 ...4
All variables are stored in memory
Depending on the size, contiguous bytes are occupied
The address of a variable is the first byte where it is stored
int i ;
char c ;
Address of i is 1
It occupies bytes 1 through 4
Address of c is 7
It only occupies byte 7
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 5 / 18
http://goforward/http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
9/34
Pointer
A pointer is a variable that stores the address of another variable
p...i
10 ... 49
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 6 / 18
http://find/http://goback/7/29/2019 poindfgrgvcfvffdvter.pdf
10/34
Pointer
A pointer is a variable that stores the address of another variable
p...i
10 ... 49
The type of a pointer depends on the type of the variable it points to
A pointer is denoted by the symbol *int i ; // i is a v a r i a b l e of type int
int * p ; // p is a p o i nt e r to int
The address of a variable is denoted by the symbol &
p = & i ; // p is now 1 ( in fi g ur e )
p stores the address of i, i.e., &i
*p denotes the content pointed by p, i.e., i
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 6 / 18
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
11/34
Pointer
A pointer is a variable that stores the address of another variable
p...i
10 ... 49
The type of a pointer depends on the type of the variable it points to
A pointer is denoted by the symbol *int i ; // i is a v a r i a b l e of type int
int * p ; // p is a p o i nt e r to int
The address of a variable is denoted by the symbol &
p = & i ; // p is now 1 ( in fi g ur e )
p stores the address of i, i.e., &i
*p denotes the content pointed by p, i.e., i
The value of a pointer can be printed using %u
The size of a pointer can be found usingsizeof
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 6 / 18
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
12/34
Pointers
# i n c l u d e < s t d i o . h >
in t m a i n ( ){
int i ;int * p ;
p r in t f (" S iz e o f p o in te r p i s % d \ n" , sizeof ( p ) ) ;
i = 5;
p = & i ;
p r in t f (" % d \t % d \t % u \t % u \n " , i , ( * p) , & i , p ) ;
* p = 7 ;p r in t f (" % d \t % d \t % u \t % u \n " , i , ( * p) , & i , p ) ;
}
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 7 / 18
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
13/34
Pointer operations
Integers can be added to or subtracted from a pointerDifferent from simple integer arithmetic
Adds (subtracts) in units of the size of the variable pointed toint * p ; // s up po se p is now 1234
p + + ; // p is now 1234 + s i ze of ( int ) = 1238 and not 1235
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 8 / 18
http://find/http://goback/7/29/2019 poindfgrgvcfvffdvter.pdf
14/34
Pointer operations
Integers can be added to or subtracted from a pointerDifferent from simple integer arithmetic
Adds (subtracts) in units of the size of the variable pointed toint * p ; // s up po se p is now 1234
p + + ; // p is now 1234 + s i ze of ( int ) = 1238 and not 1235
A pointer cannot be added to another pointer
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 8 / 18
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
15/34
Pointer operations
Integers can be added to or subtracted from a pointerDifferent from simple integer arithmetic
Adds (subtracts) in units of the size of the variable pointed toint * p ; // s up po se p is now 1234
p + + ; // p is now 1234 + s i ze of ( int ) = 1238 and not 1235
A pointer cannot be added to another pointer
Pointers cannot be multiplied or divided with integers or other
pointers
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 8 / 18
http://find/http://goback/7/29/2019 poindfgrgvcfvffdvter.pdf
16/34
Pointer operations
Integers can be added to or subtracted from a pointerDifferent from simple integer arithmetic
Adds (subtracts) in units of the size of the variable pointed toint * p ; // s up po se p is now 1234
p + + ; // p is now 1234 + s i ze of ( int ) = 1238 and not 1235
A pointer cannot be added to another pointer
Pointers cannot be multiplied or divided with integers or other
pointersA pointer can be subtracted from another pointer
Useful in arrays
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 8 / 18
P i i
http://goforward/http://find/http://goback/7/29/2019 poindfgrgvcfvffdvter.pdf
17/34
Pointer operations
Integers can be added to or subtracted from a pointerDifferent from simple integer arithmetic
Adds (subtracts) in units of the size of the variable pointed toint * p ; // s up po se p is now 1234
p + + ; // p is now 1234 + s i ze of ( int ) = 1238 and not 1235
A pointer cannot be added to another pointer
Pointers cannot be multiplied or divided with integers or other
pointersA pointer can be subtracted from another pointer
Useful in arrays
Pointers can be compared
A pointer can be assigned the address of a variable or another pointer
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 8 / 18
P i i
http://goforward/http://find/http://goback/7/29/2019 poindfgrgvcfvffdvter.pdf
18/34
Pointer operations
Integers can be added to or subtracted from a pointerDifferent from simple integer arithmeticAdds (subtracts) in units of the size of the variable pointed to
int * p ; // s up po se p is now 1234
p + + ; // p is now 1234 + s i ze of ( int ) = 1238 and not 1235
A pointer cannot be added to another pointer
Pointers cannot be multiplied or divided with integers or other
pointersA pointer can be subtracted from another pointer
Useful in arrays
Pointers can be compared
A pointer can be assigned the address of a variable or another pointerA pointer can be assigned a constant integerIt is best to avoid such assignments
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 8 / 18
P i i
http://goforward/http://find/http://goback/7/29/2019 poindfgrgvcfvffdvter.pdf
19/34
Pointer operations
Integers can be added to or subtracted from a pointerDifferent from simple integer arithmeticAdds (subtracts) in units of the size of the variable pointed to
int * p ; // s up po se p is now 1234
p + + ; // p is now 1234 + s i ze of ( int ) = 1238 and not 1235
A pointer cannot be added to another pointer
Pointers cannot be multiplied or divided with integers or other
pointersA pointer can be subtracted from another pointer
Useful in arrays
Pointers can be compared
A pointer can be assigned the address of a variable or another pointerA pointer can be assigned a constant integerIt is best to avoid such assignments
A special value NULL can be assigned to a pointerIt signifies that the pointer points to nothingIt is equivalent to 0
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 8 / 18
P i t ti I
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
20/34
Pointer operations I
# i n c l u d e < s t d i o . h >
in t m a i n ( ){
int i , j , *p , * q;
p r i nt f ( " A s s i g ni n g c o n st a n t t o a p o i nt e r \ n " ) ;p = 3 05 64 37 8; / / w ar ni ng , m ay r es ul t i n s e gm e nt a ti o n f au ltp r i nt f ( " % u \ n " , p ) ;/ / p ri nt f ( "% d \ n ", ( * p) ) ; / / m ay r es u lt i n s e gm e nt a ti o n f au lt
p r in t f (" A s si g ni n g N UL L t o a p o in t er \ n " ) ;p = NULL ;
p r i nt f ( " % u \ n " , p ) ;/ / p r i nt f ( " % d \ n " , ( * p ) ) ; / / e r ro r
p r i nt f ( " P r i n t in g a p o i nt e r \ n " ) ;p = & i ;p r i nt f ( " % u \ n " , p ) ;
p r i nt f ( " I n c r e m en t i n g a p o i nt e r \ n " ) ;p + + ;
p r in t f (" % u \t % u \n " , p , ( p - 3 ) );
p r i nt f ( " P r i n t in g a p o i nt e r \ n " ) ;q = & j ;p r i nt f ( " % u \ n " , q ) ;
// p = p * 2; // error
// q = q / p ; // error
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 9 / 18
P i t ti II
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
21/34
Pointer operations II
p r i nt f ( " C o m p a ri n g t w o p o i nt e r s \ n " ) ;p ri nt f (" % d\ t %d \ n" , ( p < q ) , ( p > q )) ;
p r i nt f ( " A s s i g ni n g a d d re s s t o p o i nt e r s \ n " ) ;p = & i ;* p = 3 ;q = & j ;* q = 9 ;p r in t f (" % u \t % u \t % d \t % d \t % d \t % d \n " , p , q , ( * p) , ( * q) , i , j ) ;
p r i nt f ( " A s s i g ni n g a p o i nt e r t o a n o th e r p o i nt e r \ n " ) ;
p = q ;p r in t f (" % u \t % u \t % d \t % d \n " , p , q , ( * p) , ( * q) ) ;
* p = 4 ;p r in t f (" % u \t % u \t % d \t % d \n " , p , q , ( * p) , ( * q) ) ;
p r i nt f ( " S u b t r a ct i n g a p o i nt e r f r om a n o th e r p o i nt e r \ n " ) ;p = q + 3;p ri nt f (" % u\ t %u \ t% d \t %d \ n" , p , q , ( p - q ) , ( q - p ) );
p r in t f (" I n c re m en t in g t he c on t en t o f a p o in te r \ n ") ;p r in t f (" % d \t % d \t % d \n " , ( * p) , ( *( p + 1 ) ) , ( * p + 1 ) );
}
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 10 / 18
Pointers and arrays
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
22/34
Pointers and arrays
Array names are essentially pointers
int a [ 8 ] ;
Array elements are stored contiguously in memory
a is a pointer to the first element of the array, i.e., a[0]
*a is equivalent to a[0]
(a + i) is a pointer to a[i]
*(a + i) is equivalent to a[i]Remember: (a + i) is actually a + i * sizeof(int)
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 11 / 18
Pointers and arrays
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
23/34
Pointers and arrays
Array names are essentially pointers
int a [ 8 ] ;
Array elements are stored contiguously in memory
a is a pointer to the first element of the array, i.e., a[0]
*a is equivalent to a[0]
(a + i) is a pointer to a[i]
*(a + i) is equivalent to a[i]Remember: (a + i) is actually a + i * sizeof(int)When a[i] is used, compiler actually computes the address of (a +i) and accesses the variable in that address
No error checking on array boundaries is done
Therefore, a[-2], a[12], etc. become legal as they are computed topossibly valid memory addresses
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 11 / 18
Pointers and arrays
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
24/34
Pointers and arrays
Array names are essentially pointers
int a [ 8 ] ;
Array elements are stored contiguously in memory
a is a pointer to the first element of the array, i.e., a[0]
*a is equivalent to a[0]
(a + i) is a pointer to a[i]
*(a + i) is equivalent to a[i]Remember: (a + i) is actually a + i * sizeof(int)When a[i] is used, compiler actually computes the address of (a +i) and accesses the variable in that address
No error checking on array boundaries is done
Therefore, a[-2], a[12], etc. become legal as they are computed topossibly valid memory addresses
Pointers can be assigned array names
int * p ;
p = a ;
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 11 / 18
Passing pointers to functions
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
25/34
Passing pointers to functions
Pointers can be passed to functions just like any other variableFunction header must indicate that the parameters are pointers
int s w a p ( int * p a , int * p b ){
int t = * p a ; * p a = * p b ; * p b = t ;
}
Important: If content of a pointer is changed inside the function, the
change is permanent, i.e., visible even outside the functionReason: When a pointer is passed as an argument, a copy of thepointer is available inside the functionHowever, the copy still points to the same addressHence, changing the content of this address changes the content of
the original addressSimilar to the situation when pointers p and q are same, i.e., p = q ;and *p is changed; *q changes automatically
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 12 / 18
Passing pointers to functions
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
26/34
Passing pointers to functions
Pointers can be passed to functions just like any other variableFunction header must indicate that the parameters are pointers
int s w a p ( int * p a , int * p b ){
int t = * p a ; * p a = * p b ; * p b = t ;
}
Important: If content of a pointer is changed inside the function, the
change is permanent, i.e., visible even outside the functionReason: When a pointer is passed as an argument, a copy of thepointer is available inside the functionHowever, the copy still points to the same addressHence, changing the content of this address changes the content of
the original addressSimilar to the situation when pointers p and q are same, i.e., p = q ;and *p is changed; *q changes automaticallyOther rules of parameter passing are followedChange in pointer itself is temporary
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 12 / 18
Swapping two integers
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
27/34
Swapping two integers
# i n c l u d e < s t d i o . h >
void s w a p ( in t * p a , in t * p b ){
int t = * p a ;* pa = * pb ;* pb = t ;
}
in t m a i n ( ){
int x = 3 , y = 4 ;int * a , * b ;
p ri nt f (" x = % d, y = % d\ n" , x , y );
/ / s wa p ( &x , & y ) ; / / e q ui v al e nt t o t he n ex t t hr ee l in es
a = & x ;b = & y ;s wa p ( a , b ) ;
p ri nt f (" x = % d, y = % d\ n" , x , y );}
pax
10 495
y ...
...
a pb... ...
...... 5325 29
b
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 13 / 18
scanf and printf
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
28/34
scanf and printf
scanf requires passing a pointer
Reason: scanf requires to change the content of the variable to what
has been typed using the keyboardTherefore, the address of the variable is passed
This is also why scanf requires & with the variable
s c an f ( % d , & i ) ;
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 14 / 18
scanf and printf
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
29/34
scanf and printf
scanf requires passing a pointer
Reason: scanf requires to change the content of the variable to what
has been typed using the keyboardTherefore, the address of the variable is passed
This is also why scanf requires & with the variable
s c an f ( % d , & i ) ;
printf does not require passing a pointerReason: printf does not require to change the content of thevariable; it just needs to print it
Therefore, only the variable is passed
This is also why printf does not require & with the variable
p r in t f ( % d , i ) ;
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 14 / 18
scanf and printf
http://find/http://goback/7/29/2019 poindfgrgvcfvffdvter.pdf
30/34
scanf and printf
scanf requires passing a pointer
Reason: scanf requires to change the content of the variable to what
has been typed using the keyboardTherefore, the address of the variable is passed
This is also why scanf requires & with the variable
s c an f ( % d , & i ) ;
printf does not require passing a pointerReason: printf does not require to change the content of thevariable; it just needs to print it
Therefore, only the variable is passed
This is also why printf does not require & with the variable
p r in t f ( % d , i ) ;
General rule: If a variable needs to be changed in a function, pass apointer that holds the address of the variable and change the contentsof the pointer
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 14 / 18
Passing arrays as pointers
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
31/34
Passing arrays as pointers
Since array names are pointers, arrays can be passed where pointersare required
To access the successive elements of the array, the pointer can beincremented
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 15 / 18
Passing arrays as pointers
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
32/34
g y p
# i n c l u d e < s t d i o . h >
void r e a d _ a r r a y ( in t *p , in t s i z e ){
int i = 0;while ( i < s ize ){
s c an f ( " % d " , p ) ;p + + ;i + + ;
}}
void p r i n t _ a r r a y ( int *p , int s i z e )
{int i = 0;while ( i < s ize ){
p r i nt f ( " % d \ t " , * p ) ;p + + ;i + + ;
}p r i n t f ( " \ n " ) ;
}
in t m a i n ( ){
int a [] = {3 , -2 , 7 , 19 };p r i n t_ a r r ay ( a , 4 ) ;r e a d_ a r r ay ( a , 4 ) ;p r i n t_ a r r ay ( a , 4 ) ;
}
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 16 / 18
Passing pointers as arrays
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
33/34
g p y
Pointers can also be passed where arrays are required
The pointer must point to the correct array addressPointer may point to the middle of the array, and operations maystart from there
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 17 / 18
Passing pointers as arrays
http://find/7/29/2019 poindfgrgvcfvffdvter.pdf
34/34
g p y
# i n c l u d e < s t d i o . h >
void r e a d _ p o i n t e r ( in t a [ ] , in t s i z e ){
int i = 0;while ( i < s ize ){
s c an f ( " % d " , & a [ i ] ) ;i + + ;
}}
void p r i n t _ p o i n t e r ( in t a [ ] , int s i z e ){
int i = 0;while ( i < s ize ){
p r i nt f ( " % d \ t " , a [ i ] ) ;i + + ;
}p r i n t f ( " \ n " ) ;
}
in t m a i n ( ){
int b [] = {3 , -2 , 7 , 19 };int * p = b ;p r i n t_ p o i nt e r ( p , 4 ) ;p r in t _p o in t er ( p + 1 , 3 ) ;r e a d _p o i n te r ( p , 4 ) ;p r i n t_ p o i nt e r ( p , 4 ) ;
}
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 18 / 18
http://find/