As we know that Array is a collection of elements of the same type, but many times we have to store the elements of the different data types. Suppose a Student record is to be stored, then for storing the record we have to group together all the information such as Roll, name, Percent which may be of different data types. For this type of situation, we use structures.

Ideally, Structure is a collection of different variables under a single name. Basically, Structures are for storing complicated data. A structure is a convenient way of grouping several pieces of related information together.

Definition of Structure in C

The structure is a composition of the different variables of different data types, grouped under the same name.

typedef struct {
char name[64];
char course[128];
int age;
int year;
} student;

Some Important Definitions of Structures Each member declared in Structure is called a member.
char name[64];
char course[128];
int age;
int year;

are some examples of members.
The name given to structure is called a tag Structure members may be of different data types including user-defined data-type also
typedef struct {
char name[64];
char course[128];
book b1;
int year;
} student;

Declaring Structure Variable in C


In C we can group some of the user-defined or primitive data types together and form another compact way of storing complicated information is called Structure. Let us see how to declare structure in a c programming language –

Syntax of Structure in C Programming

struct tag
{
data_type1 member1;
data_type2 member2;
data_type3 member3;
};

Struct keyword is used to declare a structure. Members of the structure are enclosed within the opening and closing braces.
Declaration of Structure reserves no space. It is nothing but the ― Template / Map / Shape ‖ of the structure. Memory is created, the very first time when the variable is created /Instance is created. Different Ways of Declaring Structure Variable:

Way 1: Immediately after Structure Template

struct date
{
int date;
char month[20];
int year;
}today;
// 'today' is name of Structure variable

Way 2: Declare Variables using struct Keyword

struct date
{
int date;
char month[20];
123
int year;
};

struct date today;
where ―date‖ is the name of the structure and ―today‖ is the name of the variable.


Way 3: Declaring Multiple Structure Variables

struct Book
{
int pages;
char name[20];
int year;
}book1,book2,book3;

C Structure Initialization

When we declare a structure, memory is not allocated for un-initialized variables.

Let us discuss a very familiar example of structure student, we can initialize structure variable in different ways –

Way 1: Declare and Initialize

struct student
{
char name[20];
int roll;
float marks;
}std1 = { "Pritesh",67,78.3 };

Way 2: Declaring and Initializing Multiple Variables

struct student
{
char name[20];
int roll;
float marks;
}
std1 = {"Pritesh",67,78.3};
std2 = {"Don",62,71.3};

In this example, we have declared two structure variables in above code. After declaration of
variable we have initialized two variable.
std1 = {“Pritesh”,67,78.3};
std2 = {“Don”,62,71.3};

Accessing Structure Members

Array elements are accessed using the Subscript variable, Similarly, Structure members are accessed using the dot [.] operator.
(.) is called as ―Structure member Operator‖. Use this Operator in-between “Structure name” & “member name”

#include<stdio.h>

struct Vehicle
{
int wheels;
char vname[20];
char color[10];
}v1 = {4,"Nano","Red"};
int main()
{
printf("Vehicle No of Wheels : %d",v1.wheels); 
printf("Vehicle Name : %s",v1.vname); 
printf("Vehicle Color : %s",v1.color); 
return(0);
}
Output :
Vehicle No of Wheels : 4 
Vehicle Name : Nano
Vehicle Color : Red 
Prev: Functions in c

Write a comment