Introduction to Structure in C

Preview

Introduction to Structure in C

Structure is a user defined data type provided by C language. It is a collection of heterogeneous types of elements i.e. it is a collection of different data types, size and range. It is a tool for handling a group of logically related data items.

For example, we can use a structure to represent student data which includes student’s name, marks and fees etc. Structure helps to organize a complex data in a meaningful way. The individual elements of a structure are stored in contiguous memory locations.

In C language, syntax for defining a structure is:

struct <tag_name>
{
<type1> member1;
<type2> member2;
:
:
<typeN> memberN;
};

Here, struct is the keyword which specifies that a structure is being created.

tag_name referes to the name of structure. The name of structure should follow all the rules of a legal identifier in C language.

{ Opening brace specifies that structure has started.

type1, type2,… typeN refer to data types of various elements of a structure. The data type may be predefined  data types or user defined data types of C language.

member1, member2,… memberN refer to elements declared within the structure. They are also known as members of a structure.

} Closing brace specifies that structure has ended.

; Semicolon is necessary to write after closing brace.

Example
struct data
{
int a;
float b;
};

In the above example, structure named data has been defined which contains two elements  named a and b.



Declaration of a Structure variable

We can declare variables of a structure just as we declare variables of other data types. For declaring a variable we need to write the struct keyword followed by the tag name of structure which is further followed by names of structure type variables.

In C language, syntax for declaring a structure  variable is:

struct <tag_name> var1,var2,…..varN;

Here, tag_name is the name of structure of which we want to declare variables.

var1,var2,……..varN are names of structure variables.

It should be noted that the members of a structure are not variables themselves. They don’t occupy any memory until some variable of structure is declared. Structure definition always starts with an opening brace and ends with a closing brace followed by a semicolon.

Example:

struct data d;

In the above example, variable d has been declared for already declared structure data.



Accessing structure members

We can use dot (.) operator or arrow operator  -> (Hyphen followed by greater than sign) with the structure variable to put values ad access values of members of a structure associated with a structure type variable.

The dot operator is used for normal structure variables whereas arrow operator is used to access members of a structure type pointer variable. The syntax for referring to structure member is:

struct_variable.member_name;
or
struct_variable->member_name;

Here, struct_variable is the name of structure type and member_name refers to the name of the element of structure which we are referring.

Program to demonstrate the use of structure.
#include<stdio.h>
struct data
{
int a;
float b;
};
int main()
{
struct data d;
d.a=13;
d.b=20.55;
printf(“\nd.a=%d”,d.a);
printf(“\nd.b=%f”,d.b);
return(0);
}
Output
d.a=13
d.b=20.549999
Description
In the above program, structure variable d contains 13 for its first element a and 20.55 for its second element b.

 

Initialization of a structure variable

A structure type variable can be initialized like other variables. We can use braces to assign initial values to a structure variable. The values are assigned to the members of structure variable on one to one basis starting with the very first value which is put in braces

Syntax:

struct <tag_name><struct_var>={List of values};

Example:        

struct data
{
int a;
float b;
};
struct data d={20,50.5};

In this example, structure variable d has been initialized with values 20,50.5.  20 would be stored in d.a and 50.5 would be stored in d.b .

Program to initialize a structure variable.
#include<stdio.h>
struct data
{
int a;
float b;
};
int main()
{
struct data d={3,7.8};
printf(“\nd1.a=%d”,d1.a);
printf(“\nd1.b=%f”,d1.b);
printf(“\nd.a=%d”,d.a);
printf(“\nd.b=%f”,d.b);
return(0);
}
Output
d.a=3
d.b=7.800000
Description
In the above program, structure variable d has been initialized to contain 3 for its first element a and 7.8 for its second element b.



Program to read and display a structure variable.
#include<stdio.h>
struct student
{
char name[25];
int rollno;
float fees;

};int main()
{
struct student s;
printf(“\nEnter Name of student:=”);
gets(s.name);
printf(“\nEnter rollno:=”);
scanf(“%d”,&s.rollno);
printf(“\nEnter fees:=”);
scanf(“%f”,&s.fees);printf(“\nStudent ‘s Name=%s”,s.name);
printf(“\nRollno=%d”,s.rollno);
printf(“\nFees=%f”,s.fees);
return(0);
}

Output
Enter Name of student:=Amit
Enter rollno:=1
Enter fees:=5000
Student ‘s Name=Amit
Rollno=1
Fees=5000.000000

Best Books of C





Lesson tags: accessing member of structure, create structure in c, creating structure variable in c, initialization of structuer variable, operator used with structure in c, structure operations in c, what is a structure in c
Back to: C Programming Language