ESC101N Fundamentals of Computing
Arnab Bhattacharya arnabb@[Link]
Indian Institute of Technology, Kanpur [Link]
1st semester, 2010-11 Tue, Wed, Fri 0800-0900 at L7
Arnab Bhattacharya (arnabb@[Link])
ESC101N
2010 I
1 / 14
Structures
Structures are customized data types It is declared using the keyword struct
struct Point { double x ; double y ; };
struct Point is a structure having two variables x and y Variables in a structure are called members A variable of the type structure can be dened using
struct Point p ;
Arnab Bhattacharya (arnabb@[Link])
ESC101N
2010 I
2 / 14
Structures
Structures are customized data types It is declared using the keyword struct
struct Point { double x ; double y ; };
struct Point is a structure having two variables x and y Variables in a structure are called members A variable of the type structure can be dened using
struct Point p ;
A structure type can be explicitly dened using typedef
typedef struct Point point ;
point becomes an alias for struct Point A structure variable can then simply be dened as
point s ;
Arnab Bhattacharya (arnabb@[Link]) ESC101N 2010 I 2 / 14
Members
Structures can be initialized during declaration
point 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 members structure [Link] name
p . x = 4.0; p . y = -3.0;
Members behave just like ordinary variables Size of a structure is the combined size of its members Example: Size of point is 8 + 8 = 16 bytes
Arnab Bhattacharya (arnabb@[Link])
ESC101N
2010 I
3 / 14
Functions returning structures
Since structures are variables, a function can return them
point copy_point ( point s ) { point 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 (arnabb@[Link])
ESC101N
2010 I
4 / 14
Functions returning structures
Since structures are variables, a function can return them
point copy_point ( point s ) { point 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
Dierent from array
Arnab Bhattacharya (arnabb@[Link])
ESC101N
2010 I
4 / 14
Functions returning structures
Since structures are variables, a function can return them
point copy_point ( point s ) { point 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
Dierent from array
Structures cannot be compared
if ( q == p ) // error
Arnab Bhattacharya (arnabb@[Link]) ESC101N 2010 I 4 / 14
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 modify ( point p , double c , double d ) { p.x = c; p.y = d; }
The following code prints 5.0 and 3.0
point q = {5.0 , -3.0}; modify (q , 9.0 , 1.0) ; printf ( % lf % lf \ n , q .x , q . y ) ;
Arnab Bhattacharya (arnabb@[Link])
ESC101N
2010 I
5 / 14
Pointers to structures
A pointer to a structure can be dened
point * ptr , p ; ptr = & p ;
When a pointer to structure is passed to a function, modifying the elements of the structure inside the function becomes permanent
void modify ( point *p , double c , double d ) { p->x = c; p->y = d; }
The following code prints 9.0 and 1.0
point q = {5.0 , -3.0}; modify (& q , 9.0 , 1.0) ; printf ( % lf % lf \ n , q .x , q . y ) ;
Arnab Bhattacharya (arnabb@[Link])
ESC101N
2010 I
6 / 14
Pointers to structures
A pointer to a structure can be dened
point * ptr , p ; ptr = & p ;
When a pointer to structure is passed to a function, modifying the elements of the structure inside the function becomes permanent
void modify ( point *p , double c , double d ) { p->x = c; p->y = d; }
The following code prints 9.0 and 1.0
point q = {5.0 , -3.0}; modify (& q , 9.0 , 1.0) ; printf ( % lf % lf \ n , q .x , q . y ) ;
-> notation to access members using pointers structure pointer->member name ptr->x is same as (*ptr).x
Arnab Bhattacharya (arnabb@[Link]) ESC101N 2010 I 6 / 14
Structure operations I
# include < stdio .h > # include < math .h > struct Point { double x ; double y ; }; // d e f i n i n g a s t r u c t u r e typedef struct Point point ; // defining a new type using s t r u c t u r e point new_point ( double c , double d ) // s t r u c t u r e as return value { point p ; p.x = c; p.y = d; return p ; } double distance ( point a , point b ) { double d = 0.0; // s t r u c t u r e as p a r a m e t e r
d = sqrt ( pow ( a . x - b .x , 2) + pow ( a . y - b .y , 2) ) ; return d ; }
Arnab Bhattacharya (arnabb@[Link])
ESC101N
2010 I
7 / 14
Structure operations II
void modify_wrong ( point p , double c , double d ) { p.x = c; // m o d i f y i n g members inside fu nction is t e m p o r a r y p.y = d; } void modify_p ointer ( point *p , double c , double d ) { p->x = c; // m o d i f y i n g members using s t r u c t u r e pointer is p e r m a n e n t p->y = d; // -> notation } int main () { struct Point p , q ; // d e c l a r i n g using s t r u c t u r e point s ; // d e c l a r i n g using type point t = {9.0 , -5.0}; // i n i t i a l i z i n g during d e c l a r a t i o n double d ; point * ptr ; printf ( " % lf % lf \ n " , p .x , p . y ) ; // by default , values are 0
q . x = 4.0; // a c c e s s i n g or m o d i f y i n g the members in a s t r u c t u r e q . y = -3.0; // . notation d = distance (p , q ) ; printf ( " Distance = % lf \ n " , d ) ; // p = {9.0 , -5.0}; // error
Arnab Bhattacharya (arnabb@[Link])
ESC101N
2010 I
8 / 14
Structure operations III
// printf ("% lf % lf \ n " , p .x , p . y ) ; p = new_point (7.0 , -1.0) ; printf ( " % lf % lf \ n " , p .x , p . y ) ; modify_wrong (q , 7.0 , -1.0) ; printf ( " % lf % lf \ n " , q .x , q . y ) ; ptr = & p ; modi fy_pointer ( ptr , 2.0 , -5.0) ; printf ( " % lf % lf \ t % lf % lf \ n " , p .x , p .y , ptr - >x , ptr - > y ) ; printf ( " Size of p is %d , size of ptr is % d \ n " , sizeof ( p ) , sizeof ( ptr ) ) ; // if ( q == p ) // error // printf (" Equal s t r u c t u r e s \ n ") ; }
Arnab Bhattacharya (arnabb@[Link])
ESC101N
2010 I
9 / 14
Nested structures
A structure can have another structure as its member
typedef struct Line { point p ; point q ; } line ;
Note: typedef denitions can be combined equivalent to
struct Line { point p ; point q ; }; typedef struct Line line ;
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 (arnabb@[Link]) ESC101N 2010 I 10 / 14
Array of structures
An array of structures can be simply dened as
point 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 (arnabb@[Link])
ESC101N
2010 I
11 / 14
Array of structures
# include < stdio .h > typedef struct Point { double x ; double y ; } point ; int main () { point t [3]; int i ; for ( i = 0; i < 3; i ++) { t [ i ]. x = i ; t [ i ]. y = 2 * i ; printf ( " % lf % lf \ n " , t [ i ]. x , t [ i ]. y ) ; } }
Arnab Bhattacharya (arnabb@[Link])
ESC101N
2010 I
12 / 14
Pointer in a structure
A structure can have a pointer as its member
typedef struct Student { int roll ; char * name ; } student ;
Declaring a variable of type student just declares the pointer name it does not allocate space for it
student s ; strcat ( s . name , . ) ; // error
Memory for name has to be allocated explicitly using malloc
s . name = ( char *) malloc (30 * sizeof ( char ) ) ;
student roll 10
Arnab Bhattacharya (arnabb@[Link])
name 14
... name[0] 80
2010 I 13 / 14
ESC101N
Pointer in a structure
# include < stdio .h > # include < string .h > # include < stdlib .h > typedef struct Student { int roll ; char * name ; } student ; int main () { student s ; s . name = ( char *) malloc (30 * sizeof ( char ) ) ; scanf ( " % d % s " , & s . roll , s . name ) ; printf ( " % d % s \ n " , s . roll , s . name ) ; strcat ( s . name , " A " ) ; printf ( " % d % s \ n " , s . roll , s . name ) ; free ( s . name ) ; }
Arnab Bhattacharya (arnabb@[Link])
ESC101N
2010 I
14 / 14