C Structures : Introduction | What is Structures in C

C language supports a constructed data type known as structures, a mechanism for packing data of different types. A structure is a convenient tool for handling a group of logically related data items. For example it can be used to represents a set of attributes, such as student_name, age, marks etc. The concept of a structure is analogous to that of a 'record' in many other languages. For example :

address name, door-number, street, city
book author, title, price, year
city name, country, population
customer name, telephone, city, category
time seconds, minutes, hours
date day, month, year

Structures help to organize complex data in a more meaningful way.

Definition :

A structure is a collection of variables referenced under one name, providing a convenient means of keeping related information together.

Defining a Structure :

A structure definition form a template that may be used to create structure objects (which is instance of a structure). The variables that make up the structure are called structure members. The structure members are also commonly referred to as elements or fields. The syntax of structure definition is as follows :
 struct structure_name {
   datatype member1_name;
   datatype member2_name;
   datatype member3_name;
   datatype member4_name;
   ...........
 } structure_variables;
The keyword struct tells the compiler that a structure is being declared. Also note that at the end there's option define the structure variable, at here we can either omit the structure_name or structure_variables, but not both. Now lets see an example of it.
 struct book {
   char author[30];
   char title[50];
   int price;
   int year;
 };
The above code snippet defines a structure called book, with four structure variables named author, title, price and year. At this point no structure variable has actually been created, only the form of the data has been defined.

Declaring Structure Variables :

We can declare a structure variable by using the keyword struct. The syntax is :
 struct structure_name structure_variable_name;
Example :
 struct book b_one;
The above statement declares the structure variable b_one of struct type book. We can also declare the one or more structure var‎iable at the time of structure definition, for example :
struct book {
  char author[30];
  char title[50];
  int price;
  int year;
} b_one, b_two, b_three;
The above statement declares the structure variable b_one, b_two, b_three of struct type book. It is important to understand that each structure object (or structure variable) contains its own copies of the structure's members. For example the price field of b_one is separate from the price field of b_two. Thus, changes to price field in b_one do not affect the price field in b_two.

When a structure variable (like b_one) is declared, the compiler will automatically allocates sufficient memory to accommodate all of its members. The below figure shows how the structure variable (b_one) appears in memory, and assuming 1 byte for character and 4 byte for integer.


Accessing Structure Members :

Structure members are accessed by the use of  dot (.) operator. The syntax for accessing the individual members is :
 structure_member.member_name;
Example :
 b_one.author;
The above statement gives the value stored on structure member author.

Now to assigning values to members can be done in the same way. For example :
 b_one.price = 200;
The above statement assigns the value 200 on to b_one's structure member price. Now lets see some C example code on structure :

Example 1 :
#include <stdio.h>
#include <string.h>

struct book {
  char author[30];
  char title[50];
  int price;
  int year;
};

int main() {
  struct book b_one;

  strncpy(b_one.title, "The C Programming Language", 50);
  strncpy(b_one.author, "dennis ritchie", 30);
  b_one.price = 300;
  b_one.year = 2018;

  printf("Book Detail :\n");
  printf("Title : %s\n", b_one.title);
  printf("Author : %s\n", b_one.author);
  printf("Price : %d\n", b_one.price);
  printf("Year : %d\n", b_one.year);
  return 0;
}
Output :

Book Detail :
Title : The C Programming Language
Author : dennis ritchie
Price : 300
Year : 2018

At above example the strncpy()[linked with strncpy() post] function is used to copy the string into the member variables.

Example 2 :
#include <stdio.h>
#include <string.h>

struct book {
  char author[30];
  char title[50];
  int price;
  int year;
} b_one, b_two;

int main() {
  strncpy(b_one.title, "The C Programming Language", 50);
  strncpy(b_one.author, "Dennis ritchie", 30);
  b_one.price = 300;
  b_one.year = 2018;

  strncpy(b_two.title, "The UNIX Programming Environment", 50);
  strncpy(b_two.author, "Brian W.Kernighan", 30);
  b_two.price = 259;
  b_two.year = 2015;

  printf("Book Detail :\n\n");
  printf("Title : %s\n", b_one.title);
  printf("Author : %s\n", b_one.author);
  printf("Price : %d\n", b_one.price);
  printf("Year : %d\n", b_one.year);

  printf("\nTitle : %s\n", b_two.title);
  printf("Author : %s\n", b_two.author);
  printf("Price : %d\n", b_two.price);
  printf("Year : %d\n", b_two.year);

  return 0;
}
Output :

Book Detail :

Title : The C Programming Language
Author : Dennis ritchie
Price : 300
Year : 2018

Title : The UNIX Programming Environment
Author : Brian W.Kernighan
Price : 259
Year : 2015

Next Topic :