Structure in C Programming
Pointer and Structure in C
In this lesson, we will understand what is Structure in C Programming along with some examples.
What is Structure in C
The Structure is a user-defined data type created to store different types of values under a single variable.
For example, to store the Roll, Name and Percentage of a student in a single variable, we define a Structure to hold these values. We can define the structure as:
Defining a Structure
struct record
{
int roll;
char name[20];
float per;
};
In the above program, the struct is a keyword used to define the structure, the record is the name of the defined structure, and it can be any name of your choice. The roll, name and per are the member variables to store the values in the structure.
Initialize Values to the Structure
We can also initialize the values to the member variables of the structure as:
Example
#include <stdio.h>
#include <conio.h>
struct record
{
int roll;
char name[20];
float per;
};
int main()
{
struct record x= {1,"Allen",92.63};
return 0;
}
Access the Member Variables of Structure
To access the member variables of structure, we have to use the dot (.) operator as shown below in the example.
Example
C program to store roll, name and percentage of a student using structure and display the record on the screen.
#include <stdio.h>
#include <conio.h>
#include <string.h>
struct record
{
int roll;
char name[20];
float per;
};
int main()
{
struct record x;
x.roll=1;
strcpy(x.name,"Allen");
x.per=92.36;
printf("Roll: %d\n", x.roll);
printf("Name: %s\n", x.name);
printf("Percentage: %f", x.per);
return 0;
}
Output
Roll: 1 Name: Allen Percentage: 92.360001
In the above program record is a structure and x is a structure variable of record type that can store three values in its member variable roll, name and per which can be access using the dot (.) operator as shown above.
Define Structure using typedef Keyword
The typedef keyword is used to define a new name for built-in or user-defined datatype. The datatype can be an int, float, double, char, structure or any other datatype.
Syntax of using typedef
typedef datatype new_name;
Example
#include <stdio.h>
#include <conio.h>
typedef int numeric; // numeric is a new name defined for int datatype
typedef struct record
{
numeric roll; // numeric roll means int roll
char name[20];
float per;
} rec;
int main()
{
rec x; // rec is a new name defined for record and x is a record type variable
return 0;
}
In the above example, numeric is a new name defined using a typedef keyword for int datatype. Now we can use the numeric keyword to declare integer variable as shown above.
The rec is a new name defined for record datatype and x is a record type variable in the above program.
Array of Structure
We can create an array of structure where each element of the array is a structure. See the example given below.
Example
C program to store roll, name and percentage of 3 students using array of structure and display the record on the screen.
#include <stdio.h>
#include <conio.h>
typedef struct record
{
int roll;
char name[20];
float per;
} rec;
int main()
{
rec x[3];
int i;
for(i=0; i<3; i++)
{
printf("Roll: ");
scanf("%d",&x[i].roll);
fflush(stdin);
printf("Name: ");
gets(x[i].name);
printf("Percentage: ");
scanf("%f",&x[i].per);
printf("\n");
}
printf("Roll\t\tName\t\tPer%\n");
for(i=0; i<3; i++)
{
printf("%d\t\t%s\t\t%.2f\n",x[i].roll,x[i].name,x[i].per);
}
return 0;
}
Output
Roll: 1 Name: Allen Percentage: 87.23 Roll: 2 Name: Suzen Percentage: 77.36 Roll: 3 Name: Peter Percentage: 92.14 Roll Name Per 1 Allen 87.23 2 Suzen 77.36 3 Peter 92.14
We have created an array of structure that can store records of 3 students in 3 different cells. To store and access the records in each cell of the array, we have to use its index number as shown in the above program.
Nested Structure
A structure within structure is known as Nested Structure. For example:
Example
C program to create a structure to store roll, name and a nested structure to store the marks obtained in english, math and computer.
#include <stdio.h>
#include <conio.h>
#include <string.h>
typedef struct record
{
int roll;
char name[20];
struct subjects // subject is a nested structure
{
int eng;
int math;
int comp;
} sub; // sub is a variable of subject datatype
} rec;
int main()
{
rec x;
x.roll=1;
strcpy(x.name,"Allen");
x.sub.eng=89;
x.sub.math=87;
x.sub.comp=95;
printf("Roll: %d\n", x.roll);
printf("Name: %s\n", x.name);
printf("English: %d\n", x.sub.eng);
printf("Maths: %d\n", x.sub.math);
printf("Computer: %d\n", x.sub.comp);
return 0;
}
Output
Roll: 1 Name: Allen English: 89 Maths: 87 Computer: 95
In the above program, we have created a structure record and a nested structure subject. The sub variable under record is the structure variable of the nested structure subject which is created to store the marks obtained in 3 subjects like english, maths and computer.
Structure Pointer
We can create a pointer to allocate memory for a structure. This type of pointer is known as Structure Pointer. For example:
Example
C program to show how to create and use structure pointer.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <malloc.h>
typedef struct record
{
int roll;
char name[20];
float per;
} rec;
int main()
{
rec *x;
x=(rec*)malloc(sizeof(rec));
x->roll=1;
strcpy(x->name,"Allen");
x->per=92.36;
printf("Roll: %d\n", x->roll);
printf("Name: %s\n", x->name);
printf("Percentage: %f", x->per);
return 0;
}
Output
Roll: 1 Name: Allen Percentage: 92.360001
In the above program, we have create a structure pointer using malloc() function. To access the member variables of the structure through pointer, we have to use (->) arrow operator instead of dot (.) operator as shown above.