Structure C Notes
Structure C Notes
Structure
A structure is a user defined data type. We know that arrays can be used to
represent a group of data items that belong to the same type, such as int or float. However
we cannot use an array if we want to represent a collection of data items of different types
using a single name. A structure is a convenient tool for handling a group of logically related
data items.
Structure is a user defined data type used to represent a group of data items of
different types using a single name.
struct student
{
int rollno;
char name[25];
float totalmark;
};
SSASGFGC@Hospet Page 1
Structures and Unions
Thus, the stud1 and stud2 are structure variables of type student. The above
structure can hold information of 2 students.
struct structure_name
{
type element 1;
type element 2;
……………..
type element n;
} var1,var2,…,varn;
The following single declaration is equivalent to the two declaration presented in the
previous example.
struct student
{
int rollno;
char name[25];
float totalmark;
} stud1, stud2;
Example:
In the above example stud1 is a structure variable of type student. To access the
member name, we would write stud1.name. Similarly, stud1’s rollno and stud1’s totalmark
can be accessed by writing stud1.rollno and stud1.totalmark respectively.
SSASGFGC@Hospet Page 2
Structures and Unions
#include<conio.h>
void main()
{
struct student
{
char *name;
int rollno;
float totalmark;
};
struct student stud1={"Venkat",1,98};
struct student stud3= {"Shweta",3,97};
struct student stud2={"Arpita",2,99};
clrscr();
printf(“STUDENTS DETAILS:\n”);
printf(“\n\n Roll number:%d\n Name:%s\n Total Marks:%f”, stud1.rollno, stud1.name,
stud1.totalmark);
printf(“\n\n Roll number:%d\n Name:%s\n Total Marks:%f”, stud2.rollno, stud2.name,
stud2.totalmark);
printf(“\n\n Roll number:%d\n Name:%s\n Total Marks:%f”, stud3.rollno, stud3.name,
stud3.totalmark);
getch();
}
Output
STUDENTS DETAILS:
Roll number: 1
Name: Venkat
Total Marks:98.000000
Roll number: 2
Name: Arpita
Total Marks:99.000000
Roll number: 2
Name:Shweta
Total Marks:99.000000
Array of structures:
It is possible to store a structure has an array element. i.e., an array in which each
element is a structure. Just as arrays of any basic type of variable are allowed, so are arrays
of a given type of structure. Although a structure contains many different types, the
compiler never gets to know this information because it is hidden away inside a sealed
structure capsule, so it can believe that all the elements in the array have the same type,
even though that type is itself made up of lots of different types.
SSASGFGC@Hospet Page 3
Structures and Unions
struct struct_name
{
type element 1;
type element 2;
……………..
type element n;
}array name[size];
Example:
struct student
{
int rollno;
char name[25];
float totalmark;
} stud[100];
int n,i;
clrscr();
printf("Enter total number of students\n\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter details of %d-th student\n",i+1);
printf("Name:\n");
scanf("%s",&stud[i].name);
printf("Roll number:\n");
scanf("%d",&stud[i].rollno);
printf("Total mark:\n");
scanf("%d",&stud[i].totalmark);
}
SSASGFGC@Hospet Page 4
Structures and Unions
printf("STUDENTS DETAILS:\n");
for(i=0;i<n;i++)
{
printf("\nRoll number:%d\n",stud[i].rollno);
printf("Name:%s\n",stud[i].name);
printf("Total mark:%d\n",stud[i].totalmark);
}
getch();
}
OUTPUT
Enter total number of students:
3
STUDENTS DETAILS:
Roll number:11
Name: SUBAHAS
Total mark:589
Roll number:12
Name: RUKSANA
Total mark:594
Roll number:13
Name: SANA
Total mark:595
SSASGFGC@Hospet Page 5
Structures and Unions
declaration of the embedded structure must appear before the declaration of the outer
structure. For example
#include <stdio.h>
#include <conio.h>
void main()
{
struct dob
{
int day;
int month;
int year;
};
struct student
{
struct dob d;
int rollno;
char name[25];
int totalmark;
}stud[25];
int n,i;
clrscr();
printf("Enter total number of students");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\nEnter details of %d student",i+1);
printf("\nName:");
scanf("%s",&stud[i].name);
printf("\nRoll number:");
scanf("%d",&stud[i].rollno);
printf("\nTotal mark:");
scanf("%d",&stud[i].totalmark);
printf("\nDate of birth (Format:01 06 2010):");
scanf("%d%d%d",&stud[i].d.day,&stud[i].d.month,&stud[i].d.year);
}
printf("\nSTUDENTS DETAILS:\n");
for(i=0;i<n;i++)
{
SSASGFGC@Hospet Page 6
Structures and Unions
printf("\nRoll number:%d\n",stud[i].rollno);
printf("Name:%s\n",stud[i].name);
printf("Total mark:%d\n",stud[i].totalmark);
printf("Date of birth : %d / %d / %d \n\n",
stud[i].d.day,stud[i].d.month,stud[i].d.year);
}
getch();
}
OUTPUT
Enter total number of students 2
STUDENTS DETAILS:
Roll number:12
Name: karthik
Total mark: 588
Date of birth : 11 / 12 / 1997
Roll number:18
Name: sarita
Total mark:598
Date of birth : 1 / 2 / 1997
Union
Union is a user created data type similar to structure but in this case all the members
share a common memory location. The size of the union corresponds to the length of the
largest member. Since the member share a common location they have the same starting
address.
SSASGFGC@Hospet Page 7
Structures and Unions
The real purpose of unions is to prevent memory fragmentation by arranging for a
standard size for data in the memory. By having a standard data size we can guarantee that
any hole left when dynamically allocated memory is freed will always be reusable by
another instance of the same type of union. This is a natural strategy in system
programming where many instances of different kinds of variables with a related purpose
and stored dynamically.
A union is declared in the same way as a structure. The syntax of union declaration is
union union_name
{
type element 1;
type element 2;
……………..
type element n;
};
For example, the following code declares a union data type called Student and a
union variable called stud:
union student
{
int rollno;
float totalmark;
};
union student stud;
It is possible to combine the declaration of union combination with that of the union
variables, as shown below.
union union_name
{
type element 1;
type element 2;
……………..
type element n;
}var1,var2,…,varn;
The following single declaration is equivalent to the two declaration presented in the
previous example.
union student
{
int rollno;
float totalmark;
SSASGFGC@Hospet Page 8
Structures and Unions
}x,y,z;
Structure Union
The amount of memory required to store a The amount of memory required is always equal
structure variable is the sum of the size of all the to that required by its largest member.
members.
Each member have their own memory space. One block is used by all the member of the
union.
Within a structure all members gets memory While retrieving data from a union the type
allocated; therefore any member can be that is being retrieved must be the type most
retrieved at any time. recently stored. It is the programmer's
responsibility to keep track of which type is
currently stored in a union; the results are
implementation-dependent if something is
stored as one type and extracted as another.
One or more members of a structure can be A union may only be initialized with a value
initialized at once. of the type of its first member; thus union u
described above (during example
declaration) can only be initialized with an
integer value.
Structure:
#include<stdio.h>
#include<conio.h>
void main()
{
struct testing
{
SSASGFGC@Hospet Page 9
Structures and Unions
int a;
char b;
float c;
}var;
clrscr();
printf(“\nsizeof(var) is %d”,sizeof(var));
printf(“\nsizeof(var.a) is %d”,sizeof(var.a));
printf(“\nsizeof(var.b) is %d”,sizeof(var.b));
printf(“\nsizeof(var.c) is %d”,sizeof(var.c));
var.a=10;
printf(“\nvalue of var.a is %d”,var.a);
var.b=’b’;
printf(“\nvalue of var.b is %c”,var.b);
var.c=15.55;
printf(“\nvalue of var.c is %f”,var.c);
printf(“\nvalue of var.a is %d”,var.a);
printf(“\nvalue of var.b is %c”,var.b);
printf(“\nvalue of var.c is %f”,var.c);
getch();
}
OUTPUT
sizeof(var) is 7
sizeof(var.a) is 2
sizeof(var.b) is 1
sizeof(var.c) is 4
value of var.a is 10
value of var.b is b
value of var.c is 15.550000
value of var.a is 10
value of var.b is b
value of var.c is 15.550000
Union:
#include<stdio.h>
#include<conio.h>
void main()
{
union testing
{
SSASGFGC@Hospet Page 10
Structures and Unions
int a;
char b;
float c;
}var;
clrscr();
printf(“\nsizeof(var) is %d”,sizeof(var));
printf(“\nsizeof(var.a) is %d”,sizeof(var.a));
printf(“\nsizeof(var.b) is %d”,sizeof(var.b));
printf(“\nsizeof(var.c) is %d”,sizeof(var.c));
var.a=10;
printf(“\nvalue of var.a is %d”,var.a);
var.b=’b’;
printf(“\nvalue of var.b is %c”,var.b);
var.c=15.55;
printf(“\nvalue of var.a is %f”,var.c);
printf(“\nvalue of var.a is %d”,var.a);
printf(“\nvalue of var.b is %c”,var.b);
printf(“\nvalue of var.c is %f”,var.c);
getch();
}
OUTPUT
sizeof(var) is 4
sizeof(var.a) is 2
sizeof(var.b) is 1
sizeof(var.c) is 4
value of var.a is 10
value of var.b is b
value of var.c is 15.550000
value of var.a is -13458
value of var.b is
value of var.c is 15.550000
SSASGFGC@Hospet Page 11