Structures in C

By | February 25, 2024

A structure is a user defined data type in C. A structure creates a data type that can be used to group items of possibly different types into a single type.

Structure is similar as Array, but only difference is that array allows only similar data type that can be stored in contiguous manner, but structure can stores different type of data types in non-contiguous manner.

We will learn these points in this article:

1. Syntax of struct
2. Creating structure variables
3. Initialize structure members
4. Accessing members of a structure
5. Keyword typedef
6. Important Notes 

Lets discuss further.

Structure is a group of variables of different data types represented by a single name. ‘struct’ keyword is used to create a structure.

Syntax of struct :

struct structureName 
{
    dataType member1;
    dataType member2;
    ...
}; [Structure variables name] // optional 

Creating structure variables :

Here’s how we create structure variables.

  1. By struct keyword within main() function :
    struct Books {
       char  title[50];
       char  author[50];
       char  subject[100];
       int   book_id;
    } ;
    
    int main()
    {
        struct Books book1, book2, p[20];
        return 0;
    } 
  2. Another way of creating a struct variable is. (By declaring a variable at the time of defining the structure.).
    struct Books {
       char  title[50];
       char  author[50];
       char  subject[100];
       int   book_id;
    } book1, book2, p[20]; 

    In both cases, two variables book1, book2, and an array variable p having 20 elements of type struct Book are created.

Initialize structure members :

Structure members cannot be initialized with declaration. Because when a datatype is declared, no memory is allocated for it. Memory is allocated only when variables are created.

There are three ways to do this.

  1. Using Dot(.) operator
    var_name.memeber_name = value ; 
  2. All members assigned in one statement
    struct struct_name var_name = 
    {value for memeber1, value for memeber2 ... so on for all the members} 

Example :

struct Books {
   char  title[50];  // these value 
   char  author[50];   // can not be initialize here
   char  subject[100];  // as this structure    
   int   book_id;     // but not a particulate group 
} ;

int main()
{
    struct Books book1, book2, p[20];

   // initialing member of book1 in one statement
    struct book1 = {Title of book1, name of author1, 
                        subject of book1, book1 id} ; 
    
   // initialing member of book2 using dot(.) operator
    book2.title =  Title of book2 ;
    book2.author = name of author2 ;
    book2.subject = subject of book2 ;
    book2.book_id =  book2 id;
    
    
    return 0;
} 

Accessing members of a structure :

There are two types of operators used for accessing members of a structure.

  1. Member operator (.)
    printf("%c", book1.title);  
  2. Structure pointer operator ( -> )
    printf("%c", book1->title);  

Keyword typedef :

Typedefs allow us to define types with a different name.

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

This will allow us to define a new point like this:

Books book1, book2 ; 

Example :

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

int main() {
    struct Books book1, book2;
} 

Which is equivalent to

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

int main() {
    Books book2, book2;
} 

Important Notes –

  • You can define pointers to structures in the same way as you define pointer to any other variable
  • You can pass a structure as a function argument in the same way as you pass any other variable or pointer.
  • Nesting of structures, is also permitted in C language. Nested structures means, that one structure has another structure as member variable.
  • We can also declare an array of structure variables.
  • The ‘struct’ keyword is mandatory before in declaration of a variable, in C, but it is optional in C++.



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.