C Programming - Structures & Unions

C Programming - Structures & Unions

Definition: The user defined data type which can store the data of various
data types such as int, char etc. is called Structure.
It is used to hold all the facts of single objects.
Example: Book [publication, title, author, price, page];
Student [roll no, class, name, telephone no, age];
There are 3 phases in structure.
1. Design Phase
2. Declaration phase
3. Data access phase

1. Design Phase: It specifies
a) Name of user defined data type
b) Number of member data of specified data type
c) Data type of each number variable

Example of design phase
struct book
{
int page, book no;
char title[20],author[20],publication[20];
float price;
};

2. Declaration phase: It declares a variable of user defined data type.
Example of declaration phase:
struct book
{
int page, book no;
char title[20],author[20],publication[20];
float price;
};
struct book cmp;
iii) Data Access: To access member variable of structure data type we use dot
operator.
Example of data access:
cmp.page, cmp.pageno, cmp.title, cmp.author,cmp.publication etc

Size of structure: The size of the structure variable is directly depends on its
member variable size.
sizeof(structure _name);
Example: // program to show the size of structure
#include<stdio.h>
#include<conio.h>
main()
{
struct student
{
int roll;//2 bytes
char name[30];//30*1=30 bytes
int age;//2 bytes
};
printf("%d",sizeof(student));
getch();
}
60. WAP to accept name and address of Nth number of students and sort
them on ascending/alphabetical order according to their name?
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[20],add[20],temp[20],temp1[20];
}a[100];
main( )
{
int i,j,n;
printf("Enter the no of students=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the name and address of student=");
scanf("%s%s",a[i].name,a[i].add);
}
for(i=0;i<n;i++)2
{
for(j=i+1;j<n;j++)
if(strcmp(a[i].name,a[j].name)>0) // For descending < 0
{
strcpy(a[i].temp,a[i].name);
strcpy(a[i].name,a[j].name);
strcpy(a[j].name,a[i].temp);
strcpy(a[i].temp1,a[i].add);
strcpy(a[i].add,a[j].add);
strcpy(a[j].add,a[i].temp1);
}
}
for(i=0;i<n;i++)
printf("%s\t%s\t\n",a[i].name,a[i].add);
getch( );
}
61. Write a program to input the employee name and post of five employees
and display the records in proper format using structure? [HSEB-2073-C]

www.alk.com.np

P a g e 129

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
struct emp
{
char name[50],post[50];
}e[5];
int i;
for(i=1;i<=5;i++)
{
printf("Entet the name and post of employee=");
scanf("%s%s",e[i].name,e[i].post);
}
printf("Name \t Post\n");
for(i=1;i<=5;i++)
{
printf("%s \t %s\n",e[i].name,e[i].post);
}
getch();
}

62. WAP to store name and marks of 20 students. Sort the data according
with mark in descending order?
#include<stdio.h>
#include<conio.h>
struct student
{
int mark;
char name[20];
}a[100],temp;
main( )
{
int i,j;
for(i=0;i<20;i++)
{
printf("Enter the name and marks of student=");
scanf("%s%d",a[i].name,&a[i].mark);
}
for(i=0;i<20;i++)
{
for(j=i+1;j<20;j++)
if(a[i].mark<a[j].mark)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
for(i=0;i<20;i++)
printf("%s\t%d\t\n",a[i].name,a[i].mark);
getch( );
}

Difference between:
1. Array and Structure [HSEB – 2070 supp - 10 marks]

Array Structure

1. A composite variable capable of holding
multiple data of same data type under a
single variable name is called Array.

1. The user defined data type which can
store the data of various data types such as
int, char etc. is called Structure.
2. Each data item is called element. 2. Each data item is called member.
3. It is built – in data type. 3. It is user – defined data type.
4. We can’t have array of array. 4. We can have array of structure.
5. Syntax:
Data_type array_name [size];

5. Syntax:
struct structure_name
{
datatpe memebr1 ;
datatpe memebr2 ;
......................
......................
......................
datatpe member n ;
} variable;

6. Example:
int marks [100];

6. Example:
struct student
{
int mark;
char name[20];
} a[100];

2. Structure and Union [HSEB – 2070 “D”,72 “D”, 73 – “C”]
Structure Union

1. The user defined data type which
can store the data of various data
types such as int, char etc. is called
Structure.

1. A collection of heterogeneous data
type is called Union.

2. The amount of memory required
to store a structure is equal to the
sum of the size of all the members.

2. The amount of memory required
to store a union is equal to the size
required by the largest member of
the union.

3. All the members can be accessed
at a same time.

3. Only one member can be accessed
at a time because only one member
of the union is active at a time.

4. struct keyword is used to define
structure.

4. union keyword is used to define
union.

5. Syntax:
struct structure_name
{
datatype memebr1 ;
datatype memebr2 ;
......................
datatype member n ;
} variable;

5. Syntax:
union union_name
{
datatype memebr1 ;
datatype memebr2 ;
......................
datatype member n ;
} variable;

6. Example:
struct student
{
int mark; //2 bytes
char name[20]; // 20*1=20 bytes
} a;
Memory size for a is [20+2] = 22
bytes.

6. Example:
union student
{
int mark;
char name[20];
} a;
Memory size for a is 20 bytes.

Homework Questions


C-5 (Difference between & Structure)

5.1Differentiate between structure and union with examples.[2070-D,72-
D,73-C]
5.2Differentiate between array and structure with examples. [2070-supp]
5.3Describe array, structure and pointer with examples. [2070 - D ]
5.4 Define pointer. Discuss the relation between the pointer and array with
suitable examples. [2068]
5.5WAP to store name and marks of 20 students. Sort the data according
with mark in descending order. [2066]
5.6 WAP that reads different names and address into the computer and
sorts names into alphabetical order using structure. [2061,64]