Upload
rahul-kulshrestha
View
230
Download
0
Embed Size (px)
Citation preview
7/29/2019 haledil suna
1/18
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 / 14
http://localhost/var/www/apps/conversion/tmp/scratch_6/[email protected]://www.iitk.ac.in/esc101/http://www.iitk.ac.in/esc101/http://localhost/var/www/apps/conversion/tmp/scratch_6/[email protected]://find/http://goback/7/29/2019 haledil suna
2/18
Structures
Structuresare customized data typesIt is declared using the keyword struct
struct Point{
double x ;
double y ;
};
struct Point is a structure having two variables x and yVariables in a structure are called membersA variable of the type structure can be defined using
struct P oi nt p ;
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 2 / 14
http://find/7/29/2019 haledil suna
3/18
Structures
Structuresare customized data typesIt is declared using the keyword struct
struct Point{
double x ;
double y ;
};
struct Point is a structure having two variables x and yVariables in a structure are called membersA variable of the type structure can be defined using
struct P oi nt p ;
A structure type can be explicitly defined using typedeft y pe d ef s t ru c t P o in t p oi n t ;
point becomes an alias for struct PointA structure variable can then simply be defined as
p oi nt s ;
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 2 / 14
http://find/7/29/2019 haledil suna
4/18
Members
Structures can be initialized during declaration
po int p = {4.0 , -3.0};
By default, they are initialized to 0 (or \0)
Same as array
Its members can be explicitly assigned values
. notation to access membersstructure variable.member name
p .x = 4.0;
p .y = -3.0;
Members behave just like ordinary variablesSize of a structure is the combined size of its members
Example: Size of point is 8 + 8 = 16 bytes
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 3 / 14
http://find/7/29/2019 haledil suna
5/18
Functions returning structures
Since structures are variables, a function can return them
p oi nt c o py _p oi n t ( p oi nt s )
{p oi nt p ;
p . x = s . x ;
p . y = s . y ;
return p ;
}
This can also be used to create structures
q = f (9.0 , -3.0) ;
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 4 / 14
http://find/7/29/2019 haledil suna
6/18
Functions returning structures
Since structures are variables, a function can return them
p oi nt c o py _p oi n t ( p oi nt s )
{p oi nt p ;
p . x = s . x ;
p . y = s . y ;
return p ;
}
This can also be used to create structures
q = f (9.0 , -3.0) ;
Copying can also be done simply by
q = p ;A structure is just a variable
Different from array
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 4 / 14
http://find/http://goback/7/29/2019 haledil suna
7/18
Functions returning structures
Since structures are variables, a function can return them
p oi nt c o py _p oi n t ( p oi nt s )
{p oi nt p ;
p . x = s . x ;
p . y = s . y ;
return p ;
}
This can also be used to create structures
q = f (9.0 , -3.0) ;
Copying can also be done simply by
q = p ;
A structure is just a variableDifferent from array
Structures cannot be compared
if ( q = = p ) // e rr or
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 4 / 14
http://find/7/29/2019 haledil suna
8/18
Passing structures to functions
Since structures are variables, they can be passed to functions
Modifying the elements of a structure inside a function is temporary
void m o di f y ( p o in t p , double c , double d )
{
p . x = c ;
p . y = d ;
}
The following code prints 5.0 and 3.0
po int q = {5.0 , -3.0};
modify (q , 9.0 , 1.0) ;
p ri nt f ( % l f % lf \ n , q .x , q . y );
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 5 / 14
http://find/7/29/2019 haledil suna
9/18
Pointers to structures
A pointer to a structure can be defined
p oi nt * ptr , p ;
ptr = & p;
When a pointer to structure is passed to a function, modifying theelements of the structure inside the function becomes permanent
void m o di f y ( p o in t * p , double c , double d )
{
p - > x = c ;
p - > y = d ;
}
The following code prints 9.0 and 1.0
po int q = {5.0 , -3.0};
modify (& q , 9.0 , 1.0) ;
p ri nt f ( % l f % lf \ n , q .x , q . y );
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 6 / 14
http://find/7/29/2019 haledil suna
10/18
Pointers to structures
A pointer to a structure can be defined
p oi nt * ptr , p ;
ptr = & p;
When a pointer to structure is passed to a function, modifying theelements of the structure inside the function becomes permanent
void m o di f y ( p o in t * p , double c , double d )
{
p - > x = c ;
p - > y = d ;
}
The following code prints 9.0 and 1.0
po int q = {5.0 , -3.0};
modify (& q , 9.0 , 1.0) ;
p ri nt f ( % l f % lf \ n , q .x , q . y );
-> notation to access members using pointersstructure pointer->member name
ptr->x is same as (*ptr).xArnab Bhattacharya ([email protected]) ESC101N 2010 I 6 / 14
http://find/7/29/2019 haledil suna
11/18
Structure operations I
# i n c l u d e < s t d i o . h ># i n c l u d e < m a t h . h >
struct Point
{double x ;double y ;
}; / / d e fi n in g a s t ru c tu r e
t y p ed e f s t r uc t P o in t p o i nt ; / / d e fi n in g a n ew t yp e u si ng s t ru c tu r e
p o in t n e w _ po i n t ( double c , double d ) / / s t ru c tu r e a s r e tu rn v al ue{
p oi nt p ;
p . x = c ;p . y = d ;
return p ;}
double d i st a nc e ( p oi nt a , p oi nt b ) / / s t ru c tu r e a s p a ra m et e r
{double d = 0.0;
d = s qr t( po w( a. x - b .x , 2) + p ow ( a. y - b .y , 2) );
return d ;}
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 7 / 14
http://find/7/29/2019 haledil suna
12/18
Structure operations II
void m o d i fy _ w r on g ( p o i n t p , double c , double d ){
p . x = c ; / / m o di f yi n g m e mb e rs i ns i de f u nc t io n i s t e mp o ra r y
p . y = d ;}
void m o d i fy _ p o i nt e r ( p o i nt * p , double c , double d ){
p -> x = c ; / / m o di f yi n g m e mb e rs u si ng s t ru c tu r e p o in t er i s p e rm a ne n tp -> y = d ; / / - > n ot a ti o n
}
in t m a i n ( ){
struct P oi nt p , q ; / / d e c la r i ng u s in g s t r u ct u r ep oi nt s ; / / d e cl a ri n g u si ng t yp ep oi nt t = { 9. 0 , - 5. 0} ; / / i n i t ia l i z in g d u r in g d e c la r a t io n double d ;p o in t * p t r ;
p r in t f (" % lf % l f \n " , p . x , p . y ); / / b y d ef au lt , v al u es a re 0
q .x = 4 .0 ; / / a c ce s si n g o r m o di f yi n g t he m e mb e rs i n a s t ru c tu r eq .y = - 3. 0; / / . n o ta ti o n
d = d is ta nc e (p , q ) ;p r in t f (" D i st a nc e = % l f \n " , d ) ;
// p = {9.0 , -5.0 }; // e rr or
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 8 / 14
http://find/7/29/2019 haledil suna
13/18
Structure operations III
/ / p ri nt f ( "% l f % l f \n " , p . x , p . y );
p = n e w_ p oi n t (7 .0 , - 1. 0) ;
p r in t f (" % lf % l f \n " , p . x , p . y );
m od i f y_ w r on g (q , 7. 0 , -1 .0 ) ;p r in t f (" % lf % l f \n " , q . x , q . y );
p tr = & p;m o di f y _ p oi n t e r ( pt r , 2. 0 , -5 .0 ) ;p r in t f (" % lf % l f \t % lf % l f \n " , p . x , p . y , p tr - > x , p tr - > y ) ;
p ri nt f (" S iz e of p i s % d , s iz e o f p tr i s % d \ n ", sizeof ( p ) , sizeof ( p t r ) ) ;
// if (q == p) // error
/ / p r in tf ( " E q ua l s t r uc t ur e s \ n ") ;
}
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 9 / 14
http://find/7/29/2019 haledil suna
14/18
Nested structures
A structure can have another structure as its member
t y pe d ef s t ru c t Line
{p oi nt p ;
p oi nt q ;
} line ;
Note: typedef definitions can be combined equivalent to
struct Line
{
p oi nt p ;
p oi nt q ;
};
t y pe d ef s t ru c t L in e l in e ;
Value x of point p of variable l of type line can be accessed as:l.p.x
The . operator has left-to-right associativity
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 10 / 14
http://find/7/29/2019 haledil suna
15/18
Array of structures
An array of structures can be simply defined as
p o in t t [ 3 ];
Each individual structure is accessed as t[0], etc.
A member of a structure is accessed as t[i].x, etc.
All operations allowed on normal arrays are allowed on array of
structures
It is equivalent to a pointer to structure
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 11 / 14
http://find/7/29/2019 haledil suna
16/18
Array of structures
# i n c l u d e < s t d i o . h >
t y p ed e f s t r uc t Point{
double x ;double y ;
} p oi nt ;
in t m a i n ( ){
p o in t t [ 3 ] ;int i ;
for ( i = 0; i < 3; i ++){
t [i ]. x = i ;t [ i ] . y = 2 * i ;p ri n tf ( " % lf % l f \n " , t [ i ]. x , t [ i ]. y ) ;
}}
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 12 / 14
P i i
http://find/7/29/2019 haledil suna
17/18
Pointer in a structure
A structure can have a pointer as its member
t y pe d ef s t ru c t Student
{int roll;
char * n a m e ;
} s tu de nt ;
Declaring a variable of type student just declares the pointer name
it does not allocate space for its tu de nt s ;
s t rc a t ( s . na me , . ) ; // e rr or
Memory for name has to be allocated explicitly using malloc
s .name = ( char * ) m al lo c ( 30 * sizeof ( char ) ) ;
10 14
student
...
80
name[0]roll name
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 13 / 14
P i i
http://find/7/29/2019 haledil suna
18/18
Pointer in a structure
# i n c l u d e < s t d i o . h ># i n c l u d e < s t r i n g . h ># i n c l u d e < s t d l i b . h >
t y p ed e f s t r uc t Student{
int r o l l ;char * n a m e ;
} s tu d en t ;
in t m a i n ( ){
s t ud en t s ;
s . na me = ( char * ) m a l lo c ( 3 0 * sizeof ( char ) ) ;
s c an f ( " % d % s " , & s . r ol l , s . n a me ) ;
p r in t f (" % d % s \ n" , s . ro ll , s . n am e ) ;
s t r ca t ( s . n am e , " A " ) ;
p r in t f (" % d % s \ n" , s . ro ll , s . n am e ) ;
f r e e ( s . n a m e ) ;}
Arnab Bhattacharya ([email protected]) ESC101N 2010 I 14 / 14
http://find/