Structure Variable Declaration
Structure variables can be declared in two ways:-
- Structure variable is written even when the definition of structure is written.
- The variable of the structure is also done inside the main() function.
Syntax for Structure variable outside of structure definition
struct structure_name
{
data_type member 1;
data_type member 2;
data_type member n;
}structure_variable(s);
Example:-
struct Student
{
int stu_id;
int stu_age;
char stu_name[10];
char stu_branch[10];
}info;
Syntax for structure variable in main() function.
struct structure_name
{
data_type member 1;
data_type member 2;
data_type member n;
}structure_variable(s);
int main()
{
struct structure_name structure_variable_name;
Example:-
{
int stu_id;
int stu_age;
char stu_name[10];
char stu_branch[10];
};
int main()
{
struct Student info;
}
Accessing Structure Members
It can be assigned to values in many ways. Without the structure the members of the structure have no personal meaning. To provide a value to a member of any structure, the member name must be connected with the structure variables using dot(.). The operator is also called the term or member access operator.
Syntax for Accessing Structure members:-
structure_variable_name.member_of _structure =value(optional);
#include
#include
struct Student
{
char stu_name[10];
int stu_age;
char stu_branch;
int stu_id;
};
int main()
{
struct Student s1; // s1 is a variable of Student type and
s1. stu_age=17; //age is a member of student
// using string function to add name
strcpy(s1.stu_name,"Somya");
printf("Name of student 1:%s\n", s1. stu_name);
printf("Age of student 1:%d\n", s1.stu_age);
return 0;
}
output
Name of student 1:Somya
Age of student 1:17
Structure Initialization
The structure initialization is of two type.
we can also initialize the structure variable at compile time.
TYPE 1
struct Student
{
float height;
int weight;
int age;
};
struct Student s1 = { 180.75 , 73, 23 }; //initialization
TYPE 2
struct Student s1;
s1.height = 182.5 ; //initialization of each member separately
s1.weight = 65;
s1.age = 20;
Structure working with size(sizeof)
The member of the structure allocates different memory and the size of the complete structure is as its members.
Example:
#include "stdio.h"
#include "string.h"
struct Student
{
int stu_id;
char stu_name[10];
};
int main()
{
struct Student info;
printf("size of Student id is: %d bytes\n",sizeof(info.stu_id)); //size of stu_id
printf("size of Student name: %d bytes\n",sizeof(info.stu_name)); //size of stu_name
printf("size of Student structure: %d bytes",sizeof(info)); //size of Student
return 0;
}
output
size of Student id is: 4 bytes
size of Student name: 10 bytes
size of Student structure: 16 bytes
Array of Structure
We can create an array of structure like other primitive data types. In which each element of array will represent a structure variable.
Example :
#include "stdio.h"
struct Student
{
char name[10];
int id;
};
struct Student stu[5];
int i,j;
void ask()
{
for(i=0;i<3;i++)
{
printf("enter %d Student record:\n",i+1);
printf("Student name: \t");
scanf("%s", stu[i].name);
printf("\nenter id:\t");
scanf("%d",&stu[i].id);
}
printf("\ndisplaying Student record:");
for(i=0;i<3;i++)
{
printf("Student name is %s",stu[i].name);
printf("id is %d",stu[i].id);
}
}
void main()
{
ask();
}