Thursday, February 25, 2021
More

    Structure In C Language

    Must Read

    Programmerhttp://www.improgrammer.net
    We started this site to inspire young minds to motivate and encourage them towards Programming Language. In this site you will get programming tutorials, tech, programming facts, programming fun and programming blogs.

    C arrays allow you to define the type of variables that can hold several data items of the same kind but the structure is another user-defined data type available in C programming, which allows you to combine data items of different kinds.

    C Structure is also a collection of different data types which are grouped together and each element in a structure is called a member of c language programming. If you want to access structure members and the structure variable should be declared. Many structure variables can be created for the same structure and memory will be allocated for each separately in c language.

    In c language structure is a best practice to initialize a structure to null while declaring if we don’t assign any values to structure members.

    simple different between variable, array, and structure are a normal C variable can hold only one data of one data type at a time. An array can hold a group of data of the same data type. A structure can hold a group of data of different data types.

    To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member for your program. The format of the struct statement is this:

    Syntax

    Structure tag_name
    {
    data type var_nm1;
    data type var_nm2;
    data type var_nm3;
    };

    The structure tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the structure’s definition, before the final semicolon, you can specify one or more structure variables but it is optional.

    Create a structure variable

    There are two ways for creating a structure variable Lets see by example.

    struct Books
    {
    char title[100];
    int book_id;
    };book1;
    struct struct Books
    {
    char title[100];
    int book_id;
    };
    struct Books book1;

    Accessing Structure Members 

    To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use a struct keyword to define variables of structure type. Following is the example to explain the usage of structure:

    #include<stdio.h>
    #include<string.h>
    struct Books
    {
    char title[100];
    int book_id;
    };int main( )
    {
    struct Books Book1; /* Declare Book1 of type Book */
    struct Books Book2; /* Declare Book2 of type Book */
    /* book 1 specification */
    strcpy( Book1.title, "C Programming");
    Book1.book_id = 1001;
    
    /* book 2 specification */
    strcpy( Book2.title, " Java Programming");
    Book2.book_id = 1002;
    
    /* print Book1 info */
    printf( "Book 1 title : %s\n", Book1.title);
    printf( "Book 1 book_id : %d\n", Book1.book_id);
    
    /* print Book2 info */
    printf( "Book 2 title : %s\n", Book2.title);
    printf( "Book 2 book_id : %d\n", Book2.book_id);
    
    return 0;
    }

    Output

    Book 1 title : C Programming
    Book 1 book_id : 1001
    Book 2 title : Java Programming
    book 2 book_id : 1002
    

    Structures as Function Arguments 

    You can pass a structure as a function argument in a very similar way as you pass any other variable or pointer. You would access structure variables in a similar way as you have accessed in the above example:

    #include<stdio.h>
    #include<string.h>struct Books
    {
    char title[100];
    int book_id;};/* function declaration */
    void printBook( struct Books book );
    int main( )
    {
    struct Books Book1; /* Declare Book1 of type Book */
    struct Books Book2; /* Declare Book2 of type Book *//* book 1 specification */
    strcpy( Book1.title, "C Programming");
    Book1.book_id = 1001;
    
    /* book 2 specification */
    strcpy( Book2.title, "Java Programming");
    Book2.book_id = 1002;
    
    /* 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 book_id : %d\n", book.book_id);
    }

    Output

    Book 1 title : C Programming
    Book 1 book_id : 1001
    Book 2 title : Java Programming
    book 2 book_id : 1002

    Difference Between Structure and Union

    Structure Union
    ‘struct’ keyword is used to define a structure ‘union’ keyword is used to define a union
    Syntax:

    struct struct_name{
    type element 1;
    type element 2;
    .
    .
    } variable 1, variable 2, ...;
    Syntax:

    union u_name
    {
    type element 1;
    type element 2;
    .
    .
    } variable 1, variable 2, ...;
    In the structure, all member is active at a time In the union, Only one member is active at a time
    Each member has own storage area All member share the same storage area
    all members initialized

    Example:

    #include<stdio.h>
    #include<string.h>
    struct Books
    {
    char title[100];
    int book_id;
    };
    int main( )
    {
    struct Books Book1; 
    
    /* book 1 specification */
    strcpy( Book1.title, "C Programming");
    Book1.book_id = 1001;
    
    /* print Book1 info */
    printf( "Book 1 title : %s\n", 
    Book1.title);
    printf( "Book 1 book_id : %d\n", 
    Book1.book_id);
    return 0;
    }
    

    Output:

     Book 1 title : C Programming
    Book 1 book_id : 1001
    
    only first member initialized

    Example:

    #include<stdio.h>
    #include<string.h>
    union Books
    {
    char title[100];
    int book_id;
    };
    int main( )
    {
    union Books Book1; 
    
    /* book 1 specification */
    strcpy( Book1.title, "C Programming");
    Book1.book_id = 1001;
    
    /* print Book1 info */
    printf( "Book 1 title : %s\n", 
    Book1.title);
    printf( "Book 1 book_id : %d\n", 
    Book1.book_id);
    return 0;
    }
    

    Output:

     Book 1 title : �
    Book 1 book_id : 1001
    
    Total memory required by all the member is allocated Maximum Memory required by the member is allocated
    More memory required in the structure

    Example:

    #include<stdio.h>
    #include<string.h>
    struct Books
    {
    char title[100];
    int book_id;
    };
    int main( )
    {
    struct Books Book1; /* Declare Book1 of type Book */
    
    printf( "Memory size occupied by data : %d\n", 
    sizeof(Book1));
    
    
    
    return 0;
    }

    Output

    Memory size occupied by data : 104
    
    Less memory required in the Union

    Example:

    #include<stdio.h>
    #include<string.h>
    union Books
    {
    char title[100];
    int book_id;
    };
    int main( )
    {
    union Books Book1; /* Declare Book1 of type Book */
    
    printf( "Memory size occupied by data : %d\n", 
    sizeof(Book1));
    
    
    
    return 0;
    }

    Output

    Memory size occupied by data : 100
    

    See More: How to Write C Program in Ubuntu

    Latest Articles

    More Recipes Like This