You are on page 1of 22

Structures

06/04/06

Introduction
Structures are collection of related variables sometime referred to as aggregates under one name. Structures may contain variables of many different types 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 of more complex data structures such as linked lists, queues, stacks and trees.
06/04/06 2

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.
06/04/06 3

Structures
struct student{
int roll_no; char name[64]; float marks; };

roll_no, name, marks are members of the structure.

Members of the same structure must have unique names. Each structure definition must end with a semicolon.

06/04/06

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 can be used to declare as many variables of that type as needed.
06/04/06 5

Declarations
struct { int roll_no; char name[64]; float marks; } s1, s2[4], *ptr;

In the above one declaration is done along with definition. Tag name can be omitted if the declarations are done along with definition. In the above case, variables cannot be declared separately, since tag name is missing.
06/04/06 6

Intialization
Like array intialization a structure canbe initialized with a list of constant expressions, as in: struct student s1 = {123456, ram, 24.5};

06/04/06

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;
06/04/06 8

Precedence
1) 2) 3) 4) 5)

( ) [ ] -> . ! ++ -- + - * & (type) sizeof * / %

Left to right Right to left Left to right

06/04/06

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!

06/04/06

10

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 */
06/04/06 11

Nested structures
Is the following OK?
struct nes{ double x; struct nes p; }; No, this is not allowed ! How much space this structure variable should be allotted is a problem.

06/04/06

12

Nested structures
But the following is alright !!
struct list { double item; struct list *next; }; Size calculation is not a problem. Indeed, for linked lists, trees, etc we require structures like this. We will see some simple linked lists soon.
06/04/06 13

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 approaches regarding argument passing


Pass components separately, Pass an entire structure, Pass a pointer to the structure.

Each has its good points and bad points.


06/04/06 14

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.
06/04/06 15

Structures and functions


struct point addpoint(struct point p1, struct point p2) { p1.x += p2.x; p1, p2 are automatic local variables 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 the function. Only values are passed. */
06/04/06 16

Pointers to structures
Passing a large structure to a function takes large computational resources (space, time). Instead one can pass just addresses (as we did for arrays). This is faster. Does not require to create a local copy 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.
06/04/06 17

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 *);

06/04/06

18

Arrays inside a structure


To pass an entire array (not just starting address) to a function, the way is:
Create a structure with the array as a member. Pass the structure to the function; within the structure the array is also passed. struct x { char name[64]; } y; void func(struct x a) { strcpy( a.name, hello); } y. name is not affected.
06/04/06 19

typedef
The keyword typedef provides a mechanism for creating synonyms (or aliases) for previously defined data types. typedef struct student Stud; Stud s1; /* s1 is variable of type struct student */ Stud is an alias for struct student

06/04/06

20

Tag can be omitted


typedef struct { int roll_no; char name[64]; float marks; } Stud; Stud s1, *ptr;

06/04/06

21

typedef
typedef can improve readability. Even for basic data types one can create alias names. typedef int Integer;

06/04/06

22

You might also like