1 of 22

Structures

1

12/12/2022

2 of 22

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.

2

12/12/2022

3 of 22

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.

3

12/12/2022

4 of 22

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.

4

12/12/2022

5 of 22

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.

5

12/12/2022

6 of 22

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.

6

12/12/2022

7 of 22

Intialization

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

7

12/12/2022

8 of 22

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

12/12/2022

9 of 22

Precedence

    • ( ) [ ] -> . Left to right
    • ! ++ -- + - * & (type) sizeof Right to left
    • * / % Left to right

9

12/12/2022

10 of 22

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 structure’s 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!

10

12/12/2022

11 of 22

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 */

11

12/12/2022

12 of 22

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.

12

12/12/2022

13 of 22

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.

13

12/12/2022

14 of 22

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.

14

12/12/2022

15 of 22

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.

15

12/12/2022

16 of 22

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 the function. Only values are passed. */

16

12/12/2022

p1, p2 are automatic local variables

17 of 22

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.

17

12/12/2022

18 of 22

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

18

12/12/2022

19 of 22

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.

19

12/12/2022

20 of 22

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

20

12/12/2022

21 of 22

Tag can be omitted

    • typedef struct {

int roll_no;

char name[64];

float marks;

} Stud;

Stud s1, *ptr;

21

12/12/2022

22 of 22

typedef

  • typedef can improve readability.
  • Even for basic data types one can create alias names.

typedef int Integer;

22

12/12/2022