Structure in C Programming

Structure in C programming language is a user defined data type that groups logically related information of different data types into a single unit. A structure variable can store multiple variables of different data types. Each variable declared in structure is called member.
Generally we want to store more than one information about any object. Suppose we want to store information about all employees of a company. We want to store following information for every employee:

Name (String)
Age (integer)
Salary (floating point number)
Department (String)

We can create a custom data type, which can store all data about a employee under single name.

C programming language provide support for defining a custom data type(structure) which can aggregate and store all related information about an entity. By using a structure, we can store all the details an employee into a structure variable, and we can define an array of structures to store details of all employees.

Structures in Details Click on the links below to know more about structures in C

  • Accessing Members of Structure
  • Pointers to Structures in C
  • Passing Structure to Function in C
  • Nesting of Structures in C
  • Array of Structure in C

Defining a Structure in C

Keyword struct is used to declare a structure. Declaration of a structure specifies the format/schema/template of the user defined data type. Like any other variables in C, a structure must be declared, before it’s first use in program.

Syntax of Declaring a Structure

struct structure_name
{
data_type variable_name1;
data_type variable_name2;
.
.
data_type variable_nameN;
};
  • struct keyword is used to declare a structure.
  • structure_name is the name of the structure.
  • data_type variable1; is member variable declaration statement. data_type is the type of member variable.
  • We can declare any number of member variables inside a structure.
  • Declaring a structure alone will not create any variable or allocate any memory. Memory is allocated first time when a variable of structure type is declared.

For Example

Structure declaration to store above mentioned employee details

struct employee
{
 char name[100];
 int age;
 float salary;
 char department[50];
};

Declaration of Structure Variable

We can declare variable of structure once we defined the format of structure. There are two ways of declaring a structure variable:

  • Using struct keyword after structure definition.
  • Declaring variables at the time of defining structure.

 

Declaring Variables during Structure Definition

struct employee
{
    char name[100];
 int age;
 float salary;
 char department[50];
} employee_one;

variable employee_one is an instance of structure employee.

We can declare multiple variables by separating them with comma.

struct employee
{
    char name[100];
    int age;
    float salary;
    char department[50];
} employee_one, employee_two, employee_three;

Declare Variables using struct Keyword
he syntax of defining a structure variable is similar to the variable declaration syntax of any basic data type in C. it uses struct keyword followed by structure name as the data type.

struct structure_name variable_name;
For Example
struct employee employee_one;

Above statement declares a variable of structure type employee.

All member variables of a structure are stored in contiguous memory locations.

Initialization of Structure Variable

Like any other variable in C, we can initialize a structure variable at the time of declaration. We can specify initial values as a comma separated list of constants enclosed between curly braces.

struct employee
{
    char name[100];
    int age;
    float salary;
    char department[50];
} employee_one = {"Jack", 30, 1234.5, "Sales"};

We can also initialize structure variable after structure declaration.

struct employee employee_one = {"Jack", 30, 1234.5, "Sales"};

It is not compulsory to initialize all the member variables of a structure. Following statement initializes only name member of structure employee and remaining members gets initialized with zero(for integer and float) and NULL(for pointers and char).

struct employee
{
    char name[100];
    int age;
    float salary;
    char department[50];
} employee_one = {"Jack"};