Easy Tutorial
❮ C Examples Concatenate String C Exercise Example35 ❯

C arrays allow the definition of variables that can store items of the same type. Structures are another user-defined data type available in C programming, which allows you to store items of different types.

Structures are used to represent a record. Suppose you want to track the dynamics of books in a library. You might need to track the following attributes for each book:

Defining a Structure

To define a structure, you must use the struct statement. The struct statement defines a new data type with multiple members. The format of the struct statement is as follows:

struct tag { 
    member-list
    member-list 
    member-list  
    ...
} variable-list ;

tag is the structure tag.

member-list is the standard variable definition, such as int i; or float f;, or any other valid variable definition.

variable-list is the structure variable, defined at the end of the structure, before the final semicolon. You can specify one or more structure variables. Here is how you can declare a Book structure:

struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book;

In general, tag, member-list, and variable-list must appear at least two of these three parts. Here are examples:

// This declaration declares a structure with 3 members: an integer, a character, and a double
// It also declares a structure variable s1
// This structure does not have a tag
struct 
{
    int a;
    char b;
    double c;
} s1;

// This declaration declares a structure with 3 members: an integer, a character, and a double
// The structure tag is named SIMPLE, no variables are declared
struct SIMPLE
{
    int a;
    char b;
    double c;
};
// Using the SIMPLE tag, additional variables t1, t2, and t3 are declared
struct SIMPLE t1, t2[20], *t3;

// You can also use typedef to create a new type
typedef struct
{
    int a;
    char b;
    double c; 
} Simple2;
// Now you can use Simple2 as a type to declare new structure variables
Simple2 u1, u2[20], *u3;

In the above declarations, the first and second declarations are considered by the compiler as two completely different types, even if their member lists are the same. If you try to assign t3 = &s1, it is illegal.

The members of a structure can contain other structures or pointers to their own structure type. This is often used to implement more advanced data structures like linked lists and trees.

// This structure declaration includes another structure
struct COMPLEX
{
    char string[100];
    struct SIMPLE a;
};

// This structure declaration includes a pointer to itself
struct NODE
{
    char string[100];
    struct NODE *next_node;
};

If two structures contain each other, one of them needs to be declared incompletely, as shown below:

struct B;    // Incomplete declaration of structure B

// Structure A contains a pointer to structure B
struct A
{
    struct B *partner;
    // other members;
};

// Structure B contains a pointer to structure A, declared after A
struct B
{
    struct A *partner;
    // other members;
};

Initializing Structure Variables

Like other types of variables, you can specify initial values for a structure variable at the time of definition.

Example

#include <stdio.h>

struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book = {"C Language", "tutorialpro", "Programming Language", 123456};

int main()
{
    printf("title : %s\nauthor: %s\nsubject: %s\nbook_id: %d\n", book.title, book.author, book.subject, book.book_id);
}

Output:

title : C Language
author: tutorialpro
subject: Programming Language
book_id: 123456

Accessing Structure Members

To access members of a structure, we use the member access operator (.). The member access operator is a period between the structure variable name and the structure member we want to access. You can use the struct keyword to define variables of structure type. The following example demonstrates the use of structures:

Example

#include <stdio.h>
#include <string.h>

struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};

int main( )
{
   struct Books Book1;        /* Declare Book1 of type Books */
   struct Books Book2;        /* Declare Book2 of type Books */

   /* Details of Book1 */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;

   /* Details of Book2 */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   Book2.book_id = 6495700;

   /* Print Book1 info */
   printf( "Book 1 title : %s\n", Book1.title);
   printf( "Book 1 author : %s\n", Book1.author);
   printf( "Book 1 subject : %s\n", Book1.subject);
   printf( "Book 1 book_id : %d\n", Book1.book_id);

   /* Print Book2 info */
   printf( "Book 2 title : %s\n", Book2.title);
   printf( "Book 2 author : %s\n", Book2.author);
   printf( "Book 2 subject : %s\n", Book2.subject);
   printf( "Book 2 book_id : %d\n", Book2.book_id);

   return 0;
}

When the above code is compiled and executed, it produces the following result:

Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700

Structures as Function Arguments

You can pass a structure as a function argument in the same way as you pass any other variable or pointer. You can access structure variables as shown in the above example:

Example

#include <stdio.h>
#include <string.h>

struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};

/* Function declaration */
void printBook( struct Books book );
int main( )
{
   struct Books Book1;        /* Declare Book1 of type Books */
   struct Books Book2;        /* Declare Book2 of type Books */

   /* Details of Book1 */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;

   /* Details of Book2 */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   Book2.book_id = 6495700;

   /* Print Book1 info */
   printBook( Book1 );

   /* Print Book2 info */
   printBook( Book2 );

   return 0;
}
void printBook( struct Books book )
{
   printf( "Book title : %s\n", book.title);
   printf( "Book author : %s\n", book.author);
   printf( "Book subject : %s\n", book.subject);
   printf( "Book book_id : %d\n", book.book_id);
}

When the above code is compiled and executed, it produces the following result:

Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutorial
Book book_id : 6495700

Pointers to Structures

You can define pointers to structures in the same way as you define pointers to any other variable, as follows:

struct Books *struct_pointer;

Now, you can store the address of a structure variable in the pointer variable defined above. To find the address of a structure variable, place the '&' operator before the structure's name, as follows:

struct_pointer = &Book1;

To access the members of a structure using a pointer to that structure, you must use the -> operator, as follows:

struct_pointer->title;

Let's rewrite the example above using structure pointers, which will help you understand the concept of structure pointers:

Example

#include <stdio.h>
#include <string.h>

struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};

/* Function declaration */
void printBook( struct Books *book );
int main( )
{
   struct Books Book1;        /* Declare Book1, of type Books */
   struct Books Book2;        /* Declare Book2, of type Books */

   /* Book1 details */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;

   /* Book2 details */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   Book2.book_id = 6495700;

   /* Print Book1 info by passing the address of Book1 */
   printBook( &Book1 );

   /* Print Book2 info by passing the address of Book2 */
   printBook( &Book2 );

   return 0;
}
void printBook( struct Books *book )
{
   printf( "Book title : %s\n", book->title);
   printf( "Book author : %s\n", book->author);
   printf( "Book subject : %s\n", book->subject);
   printf( "Book book_id : %d\n", book->book_id);
}

When the above code is compiled and executed, it produces the following result:

Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutorial
Book book_id : 6495700
❮ C Examples Concatenate String C Exercise Example35 ❯