Structures in C

Last Updated :

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.

Comment
Next Article
CPP_Programmer Published 22 Aug, 2020 · 4 min read
Article Tags :

Similar Reads

  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read