2. Structure is a user defined data type. It is a collection of
different data type, to create a new data type.
For example, You can define your custom type for
storing student record containing name, age and
mobile.
Creating structure type will allow you to handle all
properties of student with single variable, instead of
handling it separately.
In short it is a data structure in which we can collect
different types of data into one entity.
3. Declaration of Structure:
To declare or define a structure, we
use struct keyword.
It is a reserved word in the C .
6. Structure members can be initialized using curly braces ‘{}’.
For example, following is a valid initialization.
struct Point
{
int x, y;
};
int main()
{
// A valid initialization. member x gets value 0 and y
// gets value 1. The order of declaration is followed.
struct Point p1 = {0, 1};
}
7. Structure members are accessed using dot (.) operator.
#include<stdio.h>
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {0, 1};
// Accessing members of point p1
p1.x = 20;
printf ("x = %d, y = %d", p1.x, p1.y);
return 0;
}
Output: x = 20, y = 1
8. An array having structure as its base type is known as
an array of structure.
To create an array of structure, first structure is
declared and then array of structure is declared just like
an ordinary array.
9. struct employee
{
int emp_id;
char name[20];
char dept[20];
float salary;
};
Then an array of structure can be created like:
struct employee emp[10]; /* This is array of structure */
11. int main()
{ /* Declaration of array of structure */
struct student s[3];
int i;
for(i=0;i<3;i++)
{
printf("Enter name, roll and marks of student:n");
scanf("%s%d%f",s[i].name, &s[i].roll, &s[i].marks);
}
13. A structure can also contain array members just like normal
members such int, float
We can declare an array if we need to store multiple values
inside structure.
Structure may include as many array members as we
require.
Syntax for declaring array within structure is not different
than the conventional syntax. The only difference is that it
is declared inside the structure.
Consider the following example for storing student’s
information where we have declared an array of mark to
store marks of six subjects and display on the screen.
Since we need to store marks for six subjects, we have to
use looping structure but the same is not true for character
array of one dimension.
14. struct student
{
int rno;
char name[20]; // Character array
int mark[6]; // Integer Array with six elements
float per; };
void main()
{
struct student s1; // structure variable declaration
int tot = 0;
clrscr(); // individual member initialization.
printf(“n Enter Student Roll No :”);
scanf(“%d”, &s1.rno);
printf(“n Enter Student Name :”);
gets(s1.name);
15. // read marks for six subjects using loop n
for(i = 0 ; i < 6 ; i++ )
{
printf(“n Enter Subject %d mark :”,i+1);
scanf(“%d”, &s1.mark[i]);
tot = tot + s1.mark[i];
}
// calculate percentage
s1.per = tot/6 ;
printf(“n Roll No : %dt Name : %s”,s1.rno, s1.name);
for( i = 0 ; i < 6 ; i++ ) // display marks
{
printf(“n%d”,s1.mark[i]);
}
printf(“nTotal : %dt Percentage : %.2f”,tot, s1.per );
getch(); }
16. A union is a special data type available in C
that allows to store different data types in the
same memory location.
You can define a union with many members,
but only one member can contain a value at
any given time.
union Data {
int i;
float f;
char str[20];
} data;
17. #include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main( ) {
union Data data;
printf( "Memory size occupied by data : %dn", sizeof(data));
return 0;
}
18.
19. union Data {
int i;
float f;
char str[20];
};
int main( ) {
union Data data;
data.i = 10;
printf( "data.i : %dn", data.i);
data.f = 220.5;
printf( "data.f : %fn", data.f);
strcpy( data.str, "C Programming");
printf( "data.str : %sn", data.str);
return 0;
}
20. Structure Union
You can use a struct keyword to
define a structure.
You can use a union keyword to
define a union.
Every member within structure is
assigned a unique memory location.
In union, a memory location is
shared by all the data members.
Changing the value of one data
member will not affect other data
members in structure.
Changing the value of one data
member will change the value of
other data members in union.
The total size of the structure is the
sum of the size of every data
member.
The total size of the union is the size
of the largest data member.
You can retrieve any member at a
time.
You can access one member at a time
in the union.
It supports flexible array. It does not support a flexible array.
21. S.No Structure Union
1 Definition
Structure is heterogenous collection of
data items grouped together under a
single name
Definition
A union is a memory location that
is shared by several variables of
different datatypes
2 Syntax;
struct tagname
{
datatype member1;
datatype member2;
};
Syntax;
union tagname
{
datatype member1;
datatype member2;
};
22. 3 Eg;
struct sample{
int a;
float b;
char c;
};
Eg;
union sample
{
int a;
float b;
char c;
};
4 keyword − struct keyword − union
5 Memory allocation Memory allocation
6 Memory allocated is the sum of sizes of
all datatypes in structure
(Here, 7bytes)
Memory allocated is the
maximum size allocated among
all the datatypes in union
(Here, 4bytes)