Structure declaration in C language


The structure is a collection of different datatype variables, grouped together under a single name. It is a heterogeneous collection of data items that share a common name.

Features of structure

  • It is possible to copy the contents of all structural elements of different data types to another structure variable of its type by using an assignment operator.

  • To handle complex datatypes, it is possible to create a structure within another structure, which is called nested structures.

  • It is possible to pass an entire structure, individual elements of structure, and address of structure to a function.

  • It is possible to create structure pointers.

The general form of structure declaration is as follows −

datatype member1;
struct tagname{
   datatype member2;
   datatype member n;
};

Here,

  • struct is the keyword.

  • tagname specifies the name of a structure

  • member1, member2 specifies the data items that makeup structure.

For example,

struct book{
   int pages;
   char author [30];
   float price;
};

Structure variables

There are three ways of declaring structure variables, which are as follows −

Type 1

struct book{
   int pages;
   char author[30];
   float price;
}b;

Type 2

struct{
   int pages;
   char author[30];
   float price;
}b;

Type 3

struct book{
   int pages;
   char author[30];
   float price;
};
struct book b;

Updated on: 08-Mar-2021

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements