• 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.
The syntax of structure declaration is
struct structure_name
{
type element 1;
type element 2;
……………..
type element n;
};
• In structure declaration the keyword struct appears first, this followed by structure
name.
• The member of structure should be enclosed between a pair of braces and it defines
one by one each ending with a semicolon.
• It can also be array of structure. There is an enclosing brace at the end of declaration
and it end with a semicolon.
• We can declare structure variables as follows
struct structure_name var1,var2,…..,var n;
Example:
• To store the names, roll number and total mark of a student you can declare 3
variables.
• To store this data for more than one student 3 separate arrays may be declared.
• Another choice is to make a structure. No memory is allocated when a structure is
declared.
• It just defines the “form” of the structure. When a variable is made then memory is
allocated. This is equivalent to saying that there's no memory for “int”, but when we
declare an integer that is int var; only then memory is allocated.
• The structure for the above- mentioned case will look like
struct student
{
int rollno;
char name[25];
float totalmark;
};
• We can now declare structure variables stud1, stud2 as follows
struct student stud1,stud2;
, • Thus, the stud1 and stud2 are structure variables of type student. The above
structure can hold information of 2 students.
• It is possible to combine the declaration of structure combination with that of the
structure variables, as shown below.
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;
Accessing structure Variable
• The different variable types stored in a structure are called its members.
• The structure member can be accessed by using a dot (.) operator, so the dot operator
is known as structure member operator.
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.
Initializing Structure Members
• Structure members can be initialized at declaration.
• This much the same manner as the element of an array; the initial value must appear
in the order in which they will be assigned to their corresponding structure members,
enclosed in braces and separated by commas.
• The general form is
struct structure_name var={val1,val2,val3…..};
Example:
#include <stdio.h>
#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
Using Typedef to declare structure variables
• Using the keyword typedef we can define any type to the easiest name that we want
to use.
• Eg. : typedef struct abc node; // this defines struct abc as node
• At any place if we write node that is equivalent to struct node. Another alternative usage
of typedef is in the definition of the data type directly.
• Eg. : typedef struct student
{
char first[10];
char last[20];
} stu_type;
• We can now use the typedef as in the following declaration:
stu_type Sname, Ename;
• A structure member can be accessed by using the following format.
struct_variable_name.member_name
• Eg : printf ("%s",sname.first);
will display first (member) from Sname(structure variable).