0% found this document useful (0 votes)
4 views

c Programming 24 25 26

The document explains the use of structures and unions in programming, highlighting how to determine the size of a structure, create arrays of structures, and implement nested structures. It also discusses the advantages of passing entire structures to functions instead of individual elements and illustrates the concept of unions, which share memory among their members. Examples are provided to demonstrate the syntax and functionality of these data types.

Uploaded by

pramitsaha152
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)
4 views

c Programming 24 25 26

The document explains the use of structures and unions in programming, highlighting how to determine the size of a structure, create arrays of structures, and implement nested structures. It also discusses the advantages of passing entire structures to functions instead of individual elements and illustrates the concept of unions, which share memory among their members. Examples are provided to demonstrate the syntax and functionality of these data types.

Uploaded by

pramitsaha152
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/ 10

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

{
char name[30];

char branch[25];

int roll;h

};

void main()

struct student s[200];

int i;

s[i].roll=i+1;

printf("\nEnter information of students:");

for(i=0;i<200;i++)

printf("\nEnter the roll no:%d\n",s[i].roll);

printf("\nEnter the name:");

scanf("%s",s[i].name);

printf("\nEnter the branch:");

scanf("%s",s[i].branch);

printf("\n");

printf("\nDisplaying information of students:\n\n");

for(i=0;i<200;i++)

printf("\n\nInformation for roll no%d:\n",i+1);


printf("\nName:");

puts(s[i].name);

printf("\nBranch:");

puts(s[i].branch);

In Array of structures each element of array is of structure type as in above


example.

Array within structures

struct student

char name[30];

int roll,age,marks[5];

}; struct student s[200];

We can also initialize using same syntax as in array.

Nested structure

When a structure is within another structure, it is called Nested structure. A


structure variable can be a member of another structure and it is represented as

struct student
{

element 1;

element 2;

………

………

struct student1

member 1;

member 2;

}variable 1;

……….

……….

element n;

}variable 2;

It is possible to define structure outside & declare its variable inside other
structure.

struct date

int date,month;

};

struct student

{
char nm[20];

int roll;

struct date d;

}; struct student s1;

struct student s2,s3;

Nested structure may also be initialized at the time of declaration like in above
example.

struct student s={“name”,200, {date, month}};

{“ram”,201, {12,11}};

Nesting of structure within itself is not valid. Nesting of structure can be


extended to any level.

struct time

int hr,min;

};

struct day

int date,month;

struct time t1;

};

struct student
{

char nm[20];

struct day d;

}stud1, stud2, stud3;

Lecture Note: 25

Passing structure elements to function

We can pass each element of the structure through function but passing individual
element is difficult when number of structure element increases. To overcome this,
we use to pass the whole structure through function instead of passing individual
element.

#include<stdio.h>

#include<string.h>

void main()

struct student

char name[30];

char branch[25];

int roll;

}struct student s;

printf(“\n enter name=”);


gets(s.name);

printf("\nEnter roll:");

scanf("%d",&s.roll);

printf("\nEnter branch:");

gets(s.branch);

display(name,roll,branch);

display(char name, int roll, char branch)

printf(“\n name=%s,\n roll=%d, \n branch=%s”, s.name, s.roll. s.branch);

Passing entire structure to function

#include<stdio.h>

#include<string.h>

struct student

char name[30];

int age,roll;

};

display(struct student); //passing entire structure

void main()
{

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

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

display(s1);

display(s2);

display(struct student s)

printf(“\n name=%s, \n age=%d ,\n roll=%d”, s.name, s.age, s.roll);

Output: name=sona

roll=16

Lecture Note: 26

UNION

Union is derived data type contains collection of different data type or dissimilar
elements. All definition declaration of union variable and accessing member is
similar to structure, but instead of keyword struct the keyword union is used, the
main difference between union and structure is
Each member of structure occupy the memory location, but in the unions
members share memory. Union is used for saving memory and concept is useful
when it is not necessary to use all members of union at a time.

Where union offers a memory treated as variable of one type on one occasion
where (struct), it read number of different variables stored at different place of
memory.

Syntax of union:

union student

datatype member1;

datatype member2;

};

Like structure variable, union variable can be declared with definition or separately
such as

union union name

Datatype member1;

}var1;

Example:- union student s;

Union members can also be accessed by the dot operator with union variable and if
we have pointer to union then member can be accessed by using (arrow) operator
as with structure.
Example:-

struct

student

struct

student

int i;

char ch[10];

};struct student s;

Here datatype/member structure occupy 12 byte of location is


memory, where as in the union side it occupy only 10 byte.

You might also like