• C Programming Video Tutorials

C - Structures



A structure is a derived data type in C. In C, the struct keyword has been provided to define a custom data type. A derived or user−defined data type that groups together elements of different types. The difference between array and struct is that an array is a homogenous collection of similar types, whereas a struct can have elements of different type stored adjacently and identified by a name.

We are often required to work with values of different type but still have certain relationship between them. For example, a book is described by its name (string), price (float) and number of pages (integer). Instead of using three different variables, these values are stored in a struct variable.

Syntax

A new derived data type is defined with struct keyword as following syntax −

struct type{
   type var1;
   type var2;
   type var3;
   . . .
   . . .
};

You can then declare a variable of this derived data type as −

struct type = var;

Usually, a struct is declared before the first function is defined in the program, after the include statements. That way, the derived type can be used for declaring its variable inside any function.

Let us declare a struct type named book as follows −

struct book{
   char title[10];
   double price;
   int pages;
};

To declare a variable of this type, use the following syntax −

struct book b1;

The initialization of a struct variable is done by placing value of each element inside curly brackets.

struct book b1 = {"Learn C", 675.50, 325};

This variable is stored in the memory with the size of 22 bytes (10 for char array, 8 for double and 4 for int)

Learn C\0 675.50 325

Access struct elements

The three elements of the struct variable b1 are accessed with the dot (.) operator. Hence, b1.title refers to the title element, b1.price is the price and b1.page is the third element (number of pages)

Example

#include <stdio.h>
struct book{
   char title[10];
   double price;
   int pages;
};
int main () {
   struct book b1 = {"Learn C", 675.50, 325};
   printf("Title: %s\n", b1.title);
   printf("Price: %lf\n", b1.price);
   printf("No of Pages: %d\n", b1.pages);
   printf("size of book struct: %d", sizeof(struct book));
   return 0;
}

Output

Title: Learn C
Price: 675.500000
No of Pages: 325
size of book struct: 32

The struct type can also be defined in the following manner −

struct book{
   char title[10] ;
   double price ;
   int pages ;
   b1;
}   

Here, the type definition and variable declaration both are together.

If the struct variable is declared with this method, it cannot be initialized with curly brackets. Instead, the elements need to be assigned individually.

#include <stdio.h>
#include <string.h>
struct book{
   char title[10];
   double price;
   int pages;
} b1;
int main (){
   strcpy(b1.title, "Learn C");
   b1.price = 675.50;
   b1.pages = 325;
   printf("Title: %s\n", b1.title);
   printf("Price: %lf\n", b1.price);
   printf("No of Pages: %d\n", b1.pages);
   return 0;
}

However, the = operator can be used to assign one struct variable to other.

Let have two struct book variables, b1 and b2. The b1 variable is initialized with declaration, and we wish to assign the same values of its elements to that of b2.

We can assign individual elements as follows −

struct book b1 = {"Learn C", 675.50, 325}, b2;
   strcpy(b2.title, b1.title);
   b2.price = b1.price;
   b2.pages = b1.pages;

Note the use of strcpy() function to assign the value to a string variable instead of = operator.

However, you can also assign b1 to b2 so that all the elements of b1 are respectively assigned to elements of b2.

Example

#include <stdio.h>
#include <string.h>
struct book{
   char title[10];
   double price;
   int pages;
};
int main () {
   struct book b1 = {"Learn C", 675.50, 325}, b2;
   b2 = b1;
   printf("Title: %s\n", b2.title);
   printf("Price: %lf\n", b1.price);
   printf("No of Pages: %d\n", b1.pages);
   printf("size of book struct: %d", sizeof(struct book));  
   return 0;
}

Output

Title: Learn C
Price: 675.500000
No of Pages: 325

Pointer to struct

You can also store the address of a struct variable in the struct pointer variable.

struct book{
   char title[10];
   double price;
   int pages;
};
struct book b1 = {"Learn C", 675.50, 325},
struct book *strptr;

To store the address, use the & operator.

strptr = &b1;

C defines -> symbol to be used with struct pointer as indirection operator (also called struct dereference operator). It helps to access the elements of the struct variable to which the pointer reference to.

In this example, strptr is a pointer to struct book b1 variable. Hence, strrptr->title returns the title, similar to b1.title does.

Example

#include <stdio.h>
#include <string.h>
struct book{
   char title[10];
   double price;
   int pages;
};
int main () {
   struct book b1 = {"Learn C", 675.50, 325};
   struct book *strptr;
   strptr = &b1;
   printf("Title: %s\n", strptr->title);
   printf("Price: %lf\n", strptr->price);
   printf("No of Pages: %d\n", strptr->pages);
   return 0;
}

Output

Title: Learn C
Price: 675.500000
No of Pages: 325

It may be noted that −

  • The dot operator (.) is used to access the struct elements via the struct variable.
  • To access the elements via its pointer, we must use the indirection (->) operator.

A struct variable is like a normal variable of primary type, in the sense that you can have an array of struct, you can pass the struct variable to a function, as well as return a struct from the function.

You may have noted that you need to prefix struct type to the name of the variable or pointer at the time of declaration. This can be avoided by creating a shorthand notation with the help of typedef keyword, which we shall learn in a latter chapter.

Structures are used in different applications such as databases, file management applications, handling complex data structures such as tree and linked lists.

Advertisements