Usage of Struct

To declare a structure first we have to write the keyword “struct”. Then we have to write a user-defined name of the structure. After that, members of the structure are declared in curly braces. The end of the structure is shown by the semicolon “;”. The members are nothing but just declaring simple variables with their data types inside a structure.

Syntax of structure
struct <structure name>{                                                                          
<data type 1> <member variable name 1>;
<data type 2> <member variable name 2>;
//and so on
};

In the below example, there is a structure named student and this structure has 3 members: roll_no variable (int data type), name array of 20 index(char data type), percentage variable (float data type).

struct student{
int roll_no;
char name[20];
float percentage;
};

Here all member variables are declared as the simple variables which we are using in the main function. Means method to declare structure members is the same as declaring simple variables in the void main function.

Declaration of structure variable

 There are two types to declare structure variables. They are listed below.

With structure definition

In this method, we declare structure variables while declaring structure. After the ending of the declaration (before semicolon at the end of structure), we write the user-defined name of the structure variable as the declaration of a structure variable. Below is the syntax.

struct <structure name>{
<data type 1> <member variable name 1>;
<data type 2> <member variable name 2>;
//and so on
}<1st structure variable,2nd structure variable>; //here we can declare all structure variables

In the below example, a structure student has 3 structure variables: stu1, stu2, stu3. All of those 3 variables are declared at the end of the structure declaration (before the semicolon).

struct student{
int roll_no;
char name[20];
float percentage;
}stu1,stu2,stu3;
Using structure tag

In this method, we do not declare structure variables while declaring structure. But we declare the structure variable in that block, where we want to use it. To declare it, we first write the keyword “struct”, then we write the name of a structure, then we write a user-defined structure variable name and end it with a semicolon.

struct <structure name>{
<data type 1> <member variable name 1>;
<data type 2> <member variable name 2>;
//and so on
};
//below variable declaration we can do globally (outside function) or locally (inside function)
struct   <structure name>   <1st structure variable>;
struct   <structure name>   <2nd structure variable>;
//here we can declare all structure variables

Below is an example of a structured student. It has three structure variables: stu1, stu2, stu3. We can declare structure variables together using a common structure tag (like stu1 and stu2 in the below example).
We can declare structure variables using different tags also (like stu1 and stu3 or stu2 and stu3 in the below example).

struct student{
int roll_no;
char name[20];
float percentage;
};
struct student stu1,stu2;
struct student stu3;

Syntax of accessing structure members

Syntax to access a member of a structure is as listed below.

<structure variable name, whose member we want to access>.<member name which we want to access>

Let’s take an example of a structure whose name is stu. It has an int variable member roll_no. To access it we use the above syntax. The below example is to give any value to member roll_no manually. But if we want to print the value then we write stu1.roll_no instead of the variable name. Same if we want to get the value from the user of roll_no then we write & stu1.roll_no instead of &variable name.

// if stu1 is variable of structure
struct stu{
int roll_no;
}stu1;
.
.                       // code
.
stu1.roll_no = <any value that we want to give to roll_no>

Initialization of structure variable

There are two methods to initialize a variable of structure.

Method 1: when we have used structure declaration With structure definition

In this method, we give value to a variable with a declaring structure variable and we separate values by comma.
In the below example, stu1 is a variable of structure student. By writing stu1={12,”Ram”,77.6}, it means that 12 value is given to roll_no, Ram value is given to name and 77.6 is given to percentage member of stu1. Same for stu2={24,”Raman”,87.9} and stu3={36,”Om”,69.4}.

struct student{
int roll_no;
char name[20];
float percentage;
}stu1={12,”Ram”,77.6},stu2={24,”Raman”,87.9},stu3={36,”Om”,69.4};

But here values should be in sequence, which means which value we had written first ,it is given to the 1st member and so on.

Method 2: When we have used structure declaration Using a structure tag

In this method, we give values while declaring structure variables using the structure tag. This does the same work explained in the above method. But this is only used when we declare a structure Using structure tag.

struct student{
int roll_no;
char name[20];
float percentage;
};

struct student stu1={12,”Ram”,77.6},stu2={24,”Raman”,87.9};
struct student stu3={36,”Om”,69.4};

Here also values should be in sequence.

Method 3: Accessing structure members

In this method, there is nothing related to the sequence. We can do this while compiling time access and run-time access while accessing structure members.

Compile time access

struct student{
int roll_no;
char name[20];
float percentage;
}stu1,stu2,stu3; // these 3 variable can be declared using structure tag also

//code lines
stu1.roll_no=12,strcpy(stu1.name,”Ram”),stu1.percentage=77.6;

//here to copy string we have to use string function, direct we can not give value to string
stu2.roll_no=24,strcpy(stu2.name,”Raman”),stu2.percentage=87.9;

stu3.roll_no=36,strcpy(stu3.name,”Om”),stu3.percentage=69.4;

Run time access

struct student{
int roll_no;
char name[20];
float percentage;
}stu1,stu2,stu3; // these 3 variable can be declared using structure tag also

//code lines

scanf(“%d %s %f”,&stu1.roll_no,stu1.name,&stu1.percentage);
scanf(“%d %s %f”,&stu2.roll_no,stu2.name,&stu2.percentage);
scanf(“%d %s %f”,&stu3.roll_no,stu3.name,&stu3.percentage);
Example program to understand how to use all the above methods

In the below example, there is a structured student. It has 3 members: roll –no, name, percentage. There are 3 structure variables: stu1, stu2, stu3. Here we have used all 3 methods of Initialization of structure variables. For stu1 we have used method1, method2 for stu2 and method3 for stu3.

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student{
    int roll_no;
    char name[20];
    float percentage;
}stu1={12,"Ram",77.6}; //method 1
void main()
{	clrscr();
    struct student stu2={24,"Raman",87.9}; //method 2
    struct student stu3;
    strcpy(stu3.name,"Om");
    stu3.roll_no=36,stu3.percentage=69.4; // method 3 complie time
    struct student stu4; 
    scanf("%d %s %f",&stu4.roll_no,stu4.name,&stu4.percentage); // method 3 run time
    //now displaying all values
    printf("stu1 : %d %s %f \n",stu1.roll_no,stu1.name,stu1.percentage);
    printf("stu2 : %d %s %f \n",stu2.roll_no,stu2.name,stu2.percentage);
    printf("stu3 : %d %s %f \n",stu3.roll_no,stu3.name,stu3.percentage);
    printf("stu4 : %d %s %f \n",stu4.roll_no,stu4.name,stu4.percentage);
    getch();
}

Output:

 

 

 

 

 

Lesson learned from this blog:

1 ) Syntax of structure.
2) Syntax of accessing members of a structure.
3 ) Types of structure variable declaration.
4 ) Types of structure variable initialization.
5 ) How to make a program using structure.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments