18 structures
TRANSCRIPT
-
8/6/2019 18 Structures
1/22
06/04/06 1
Structures
-
8/6/2019 18 Structures
2/22
06/04/06 2
Introduction
Structures are collection of related variables sometime referred to as aggregates under onename.
Structures may contain variables of many differenttypes in contrast to arrays that contain only
elements of the same data type. Structures are commonly used to define records to
be stored in files.
Pointers and structures facilitate the formation ofmore complex data structures such as linked lists,queues, stacks and trees.
-
8/6/2019 18 Structures
3/22
06/04/06 3
Structure definitions Structures are derived data types they are
constructed using objects of other types.
Structure definition:
struct student{int roll_no;
char name[64];
float marks;};
keyword struct introduces the structure definition.
The identifier student is the structure tag. struct student s1, s2;
In the above declaration s1 and s2 are data
items of type struct student.
-
8/6/2019 18 Structures
4/22
-
8/6/2019 18 Structures
5/22
06/04/06 5
Declarations
struct student{
int roll_no;char name[64];
float marks;
};/*this is structure definition */struct student s1, s2[4], *ptr;
/*declaration of variables of type struct student */
struct student is a user defined type and canbe used to declare as many variables of thattype as needed.
-
8/6/2019 18 Structures
6/22
-
8/6/2019 18 Structures
7/22
06/04/06 7
Intialization
Like array intialization a structure canbe initialized
with a list of constant expressions, as in: struct student s1 = {123456, ram, 24.5};
-
8/6/2019 18 Structures
8/22
06/04/06 8
Accessing members of a structure
struct student{
int roll_no;
char name[64];
float marks;
}s1, s2[4], *ptr;
s1.roll_no = 123456;
strcpy(s1.name, Ram);
s2[0].roll_no = 234123;
s2[0].marks = -1.0;
ptr = &s2[1];
ptr ->roll_no = 987654;
-
8/6/2019 18 Structures
9/22
06/04/06 9
Precedence
1) ( ) [ ] -> . Left to right
2) ! ++ -- + - * & (type) sizeof Right to left3) * / % Left to right
4)
5)
-
8/6/2019 18 Structures
10/22
06/04/06 10
Operations with structures
The only legal operations on a structure are
Assignment to the same typed structures. Taking its address with &
Accessing its members.
Using sizeof operator to find a structures size struct student s1, s2, *ptr;
if (s1 == s2) { . } /* not allowed */
ptr -> roll_no (*ptr).roll_no
Note, parentheses are a must in the above!
-
8/6/2019 18 Structures
11/22
06/04/06 11
Nested structures
Structures can be nested
struct point {double x; double y;
};
struct rect {
struct point p1, p2;
};struct rect screen;
screen.p1.x = 0.0; /* OK */
-
8/6/2019 18 Structures
12/22
06/04/06 12
Nested structures
Is the following OK?
struct nes{double x;
struct nes p;
};
No, this is not allowed !
How much space this structure variable shouldbe allotted is a problem.
-
8/6/2019 18 Structures
13/22
-
8/6/2019 18 Structures
14/22
06/04/06 14
Structures and functions
Structures can be passed as arguments and
can be returned from functions. This is in contrast to arrays.
There are at least three possible approachesregarding argument passing
Pass components separately,
Pass an entire structure, Pass a pointer to the structure.
Each has its good points and bad points.
-
8/6/2019 18 Structures
15/22
06/04/06 15
Structures and functions
struct point { double x, y; };
struct point makepoint(double, double);
int main( ){
struct point s;
s = makepoint( 10 , 20.5);
}struct point makepoint(double x, double y)
{
struct point temp;
temp.x = x; temp.y = y;return temp;
}
This is illustrating that a structure can be returned from a function and it can
be assigned to the same typed variable.
-
8/6/2019 18 Structures
16/22
06/04/06 16
Structures and functions
struct point addpoint(struct point p1, struct point p2)
{
p1.x += p2.x;
p1.y += p2.y;
return p1;
}
struct point p, q, x;
p = makepoint(0,10);
q = makepoint(100, 50);
x = addpoint(p, q); /* p, q are not modified by thefunction. Only values are passed.
*/
p1, p2 are automatic local variables
-
8/6/2019 18 Structures
17/22
06/04/06 17
Pointers to structures
Passing a large structure to a function takes largecomputational resources (space, time).
Instead one can pass just addresses (as we didfor arrays).
This is faster. Does not require to create a localcopy for the entire structure.
But, one should be careful; now the function can
access the original variable. Const pointers is a good remedy for these
problems.
-
8/6/2019 18 Structures
18/22
06/04/06 18
Pointers, structures and functions
double norm(struct point *);
double norm(struct point *p)
{
return( sqrt(p->x * p->x + p->y * p->y) );
}
it might be better to use const, as in:
double norm(const struct point *);
-
8/6/2019 18 Structures
19/22
06/04/06 19
Arrays inside a structure
To pass an entire array (not just starting address) toa function, the way is: Create a structure with the array as a member.
Pass the structure to the function; within the structure thearray is also passed.
struct x {char name[64];
} y;
void func(struct x a){
strcpy( a.name, hello);
}
y. name is not affected.
-
8/6/2019 18 Structures
20/22
06/04/06 20
typedef
The keyword typedefprovides a mechanism
for creating synonyms (or aliases) forpreviously defined data types.
typedef struct student Stud;
Stud s1; /* s1 is variable of type struct
student */
Stud is an alias for struct student
-
8/6/2019 18 Structures
21/22
06/04/06 21
Tag can be omitted
typedef struct {
int roll_no;char name[64];
float marks;
} Stud;
Stud s1, *ptr;
-
8/6/2019 18 Structures
22/22
06/04/06 22
typedef
typedef can improve readability.
Even for basic data types one can createalias names.
typedef int Integer;