Structures and Unions
Structures and Unions
and Technology
Placement Training
on
Structures and Unions
By
Ashwini S S
Assistant Professor
Department of ISE
NCET
1
Structures
A structure is a user defined data type in C/C++. A structure
creates a data type that can be used to group items of possibly
different types into a single type.
It can have integer, float, double or character data in it.
struct structureName
{
dataType member1;
dataType member2;
...
}; 2
struct address
{
char name[50];
char street[100];
char city[50];
char state[20];
int pin;
};
3
Create struct Variables
When a struct type is declared, no storage or memory is
allocated. To allocate memory of a given structure type and
work with it, we need to create variables.
struct Person
{
char name[50];
int id;
float salary;
};
int main()
{
struct Person person1, person2, p[20];
return 0;
}
4
Another way of creating a struct variable is:
struct Person
{
char name[50];
int citNo;
float salary;
} person1, person2, p[20];
int main()
{
struct Point p1 = {0, 1};
}
A valid initialization. member x gets value 0 and y
gets value 1. The order of declaration is followed. 7
How to access structure elements?
Members of the structure are accessed using dot(.)
operator.
struct Point
Output:
{
int x, y; x=20, y=1
};
int main()
{
struct Point p1 = {0, 1};
p1.x = 20;
printf ("x = %d, y = %d", p1.x, p1.y);
return 0;
} 8
Keyword typedef
We use the typedef keyword to create an alias name
for data types. It is commonly used with structures
to simplify the syntax of declaring variables.
union car
{
char name[50];
int price;
};
10
Difference between Structures and
Unions
#include <stdio.h>
union unionJob
{
char name[32];
float salary;
int workerNo;
} uJob;
struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;
11
int main()
{
printf("size of union = %d bytes", sizeof(uJob));
printf("\nsize of structure = %d bytes", sizeof(sJob));
return 0;
}
Output:
size of union =
size of structure =
12
• 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.
13
Array of Structure
Structure is collection of different data type.
An object of structure represents a single
record in memory, if we want more than one
record of structure type, we have to create an
array of structure or object. As we know, an
array is a collection of similar type, therefore
an array can be of structure type.
14
#include<stdio.h>
struct Employee
{
int Id;
char Name[25];
int Age;
long Salary;
};
void main()
{
int i;
struct Employee Emp[ 3 ];
15
for(i=0;i<3;i++)
{
printf("\nDetails of Employees");
for(i=0;i<3;i++)
printf("\n%d\t%s\t%d\t%ld",Emp[i].Id,Emp[i].Name,Emp[i].Age,Emp[i].Salary);
16
Structure using pointer
17
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[30];
float percentage;
};
int main()
{
int i;
struct student record1 = {1, "Raju", 90.5};
struct student *ptr;
ptr = &record1;