Structure in c programming


In this tutorial, you will learn about structure in c programming and how they are used in c programs.

c programming structure

C programming structures

Similar to array structures are used to represent a collection of data items but of similar and different types using the single name. Structure is a user defined datatype where we have to design and declare a data structure before the variable of that type are declared and used. So it can be said that structures help to organize complex data in a meaningful way.

General format of structure


  struct tag_name{
    data_type member1;
    data_type member2;
    ...........
    ...........
    data_type member(n);
  };

Here the keyword struct declares a structure that will hold the datafields named members. ‘tag_name’ is a name of the structure that we have declared. Inside the structure, we can use any kind of datatypes like int, float, char and others.

Things to remember
  • The structure template must terminate with a semicolon.
  • That tag_name can be used to declare structure variable of its type later in the program

 

Declaring structure variable

Now that we have defined and designed structure, next step is to declare a structure variable in programs. Here is how it is done:

struct tag_name variable1, variable2;

Here variable1 and variable2 are variables declared of type struct tag_name. Each one of these variables will hold members defined inside the structure.

Example :


  struct product_details
  {
    char product_name[20];
    int product_number;
  };
  struct product_details product1, product2;

Here product1 and product2 are variable which will hold members product_name and product_number.
NOTE: Remember that members inside the structure are not variables and they do not occupy memory until they are associated with structure variables such as product1 and product2.

 

Another way to declare structure variable is without using tagname:


struct{
    data_type member1;
    data_type member2;
    .........
} name1, name2, name3;

Here name1, name2, and name3 are structure variables representing members but such declaration has no tag_name.
This method is not recommended because, without a tagname, we cannot use structure variable of its type for future declarations.

Accessing structure members

As we already know members themselves are not variables and they don’t hold any meaning and memory unless they are associated with a structure variable. So to access those members member operator ‘.’ is used which is also called dot operator.
For example:

product1.product_name;

Example to highlight concept of structure in C programming

Program to take employee, his/her name, age, and salary.

#include <stdio.h> 
struct employee{    // structure definition
  char name[20];
  int age;
  int salary;
}
int main()
{
  struct employee person; // declaration of structure variable person
  printf("Enter name,age and salary \n");
  scanf("%s %d %d", person.name, person.age, person.salary);
  printf("%s %d %d \n", person.name, person.age, person.salary);
  return 0;
}

Output:

Enter name,age and salary
Joe 27 90000

Structures within structures

Structure within structure means nesting of the structure where one struct variable is used in another structure definition or a new structure is defined inside the structure.

Examples:

struct book
{
  char name[20];
  char author[20];
  struct
  {
  int price;
  int pages;
 } book_details;
}:

Here book structure contains a member named book_details which are itself structure with two members.

Another way to write nested structures is:

struct book_details
  {
  int price;
  int pages;
 };
struct book
{
  char name[20];
  char author[20];
  struct book_details intro;
};