Structures and Unions in C Lan
Structures and Unions in C Lan
Declaring
structure variable struct student report; struct student *report, rep;
USES OF C STRUCTURES:
1. C Structures can be used to store huge data. Structures act as a database.
2. C Structures can be used to send data to the printer.
3. C Structures can interact with keyboard and mouse to store the data.
4. C Structures can be used in drawing and floppy formatting.
5. C Structures can be used to clear output screen contents.
6. C Structures can be used to check computer’s memory size etc.
C – Array of Structures
As you know, C Structure is collection of different datatypes ( variables ) which are grouped together. Whereas, array of structures is
nothing but collection of structures. This is also called structure array in C.
Syntax : Struct structname objname[size];
Example : struct student a[10];
C – Passing struct to function
• A structure can be passed to any function from main function or from any sub function.
• Structure definition will be available within the function only.
• It won’t be available to other functions unless it is passed to those functions by value or by address(reference).
• Else, we have to declare structure variable as global variable. That means, structure variable should be declared outside the main
function. So, this structure will be visible to all the functions in a C program.
SYNTAX : FUNCTIONNAME(OBJ);
C – Structure using Pointer C structure can be accessed in 2 ways in a C program. They are,
1. Using normal structure variable 2. Using pointer variable
Dot(.) operator is used to access the data using normal structure variable and arrow (->) is used to access the data using pointer
variable.
Self referential structure: any Structure contains same Structure pointer variable
Struct student
{
Int sno;
Char sna[40],dept[10];
Struct student *p;
};
C – Nested Structure : Nested structure in C is nothing but structure within structure. One structure can be declared inside other
structure as we declare structure members inside a structure. The structure variables can be a normal structure variable or a pointer
variable to access the data.
Syntax: Struct structurename
{
Datatype var1,va2,..;
Struct structurename obj1,obj2…;
}
C – Typedef : Typedef is a keyword that is used to give a new symbolic name for the existing name in a C program. This is same like
defining alias for the commands.
Syntax : typedef datatype aliasname ;
example: typedef int number;
typedef struct student stud;
When we use “typedef” keyword before struct <tag_name> like above, after that we can simply use type definition “stud” in the C
program to declare structure variable.
Example program accept students data and print using structures and pointers
#include<stdio.h>
#include<stdlib.h>
struct student
{
Int rno;
Char na[40],d[10];
Long int fee;
};
void main()
{
struct student *a;
a=(struct student *) malloc (sizeof(struct student));
printf("\n\t Enter Student Roll Number : ");
scanf("%d",&a->rno);
printf("\t Enter Student name : ");
scanf("%s",a->na);
printf("\t Enter Student dept : ");
scanf("%s",a->d);
printf("\t Enter Student Fee : ");
scanf("%ld",&a->fee);
printf("\n\t Student Information is ");
printf("\n\t %d \t %s \t %s \t %ld",a->rno,a->na,a->d,a->fee);
}
C – Union C Union is also like structure, i.e. collection of different data types which are grouped together. Each element in a union is
called member.
• Union and structure in C are same in concepts, except allocating memory for their members.
• Structure allocates storage space for all its members separately.
• Whereas, Union allocates one common storage space for all its members
• We can access only one member of union at a time. We can’t access all member values at the same time in union. But, structure
can access all member values at the same time. This is because, Union allocates one common storage space for all its members.
Where as Structure allocates storage space for all its members separately.
• Many union variables can be created in a program and memory will be allocated for each union variable separately.
• Below table will help you how to form a C union, declare a union, initializing and accessing the members of the union.
Type Using normal variable Using pointer variable
Declaring
union variable union student report; union student *report, rep;
Structure occupies higher memory space. Union occupies lower memory space over structure.
We can access all members of structure at a time. We can access only one member of union at a time.