0% found this document useful (0 votes)
16 views5 pages

Unit 4

module 4 contains structures
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Unit 4

module 4 contains structures
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Sructure

It is the collection of dissimilar data types or heterogenous data types grouped


together. It means the data types may or may not be of same type.

Structure declaration-

struct tagname

Data type member1;

Data type member2;

Data type member3;

………

………

Data type member n;

};

OR

struct

Data type member1;

Data type member2;

98 *Under revision
Data type member3;

………

………

Data type member n;

};

OR

struct tagname

struct element 1;

struct element 2;

struct element 3;

………

………

struct element n;

};

Structure variable declaration;

struct student

int age;

char name[20];

char branch[20];
99 *Under revision
}; struct student s;

Initialization of structure variable-

Like primary variables structure variables can also be initialized when they are
declared. Structure templates can be defined locally or globally. If it is local it can
be used within that function. If it is global it can be used by all other functions of
the program.

We cant initialize structure members while defining the structure

struct student

int age=20;

char name[20]=”sona”;

}s1;

The above is invalid.

A structure can be initialized as

struct student

int age,roll;

char name[20];

} struct student s1={16,101,”sona”};

struct student s2={17,102,”rupa”};

If initialiser is less than no.of structure variable, automatically rest values are taken
as zero.

100 *Under revision


Accessing structure elements-

Dot operator is used to access the structure elements. Its associativety is from left
to right.

structure variable ;

s1.name[];

s1.roll;

s1.age;

Elements of structure are stored in contiguous memory locations. Value of


structure variable can be assigned to another structure variable of same type using
assignment operator.

Example:

#include<stdio.h>

#include<conio.h>

void main()

int roll, age;

char branch;

} s1,s2;

printf(“\n enter roll, age, branch=”);

scanf(“%d %d %c”, &s1.roll, &s1.age, &s1.branch);

s2.roll=s1.roll;

printf(“ students details=\n”);

printf(“%d %d %c”, s1.roll, s1.age, s1.branch);

printf(“%d”, s2.roll);

101 *Under revision


}

Unary, relational, arithmetic, bitwise operators are not allowed within structure
variables.

Lecture Note:24

Size of structure-

Size of structure can be found out using sizeof() operator with structure variable
name or tag name with keyword.

sizeof(struct student); or

sizeof(s1);

sizeof(s2);

Size of structure is different in different machines. So size of whole structure may


not be equal to sum of size of its members.

Array of structures

When database of any element is used in huge amount, we prefer Array of


structures.

Example: suppose we want to maintain data base of 200 students, Array of


structures is used.

#include<stdio.h>

#include<string.h>

struct student

102 *Under revision

You might also like