-->

Learn to code the on-demand coding Languages in an easy way. We help you understand programming concepts and coding Techniques Better.

Monday, February 8, 2021

What is a Structure in C | Pointers to Structures | Structure example in C

Structure in C:


Structure: Structure in C is a user-defined data type that enables us to store the collection of data of different types. Whereas arrays in C can hold several data items of the same kind. Each element of a structure is called a member. A structure can contain any type of data such as int, char, float, array, and pointer.

Syntax: The struct keyword is used to define a structure.

    struct  struct_name
    {
        data_type  member1;
        data_type  member1;
        data_type  member1;
       .
        .
        .
        .
    }var1,var2,....;

    Don't forget to put the semicolon at the end of the structure.

    struct: The struct keyword is used as a type of structure.
    
    struct_name: We can give any name as a structure name during its declaration.

    var1, var2: Here var1, var2 are the variables to access the data members. We can create any number of variables/Instances.

Declaration: Let's see an example to define a structure for an employee.

    struct  employee
    {
        int id;
        
        char name[10];

        int b;

    }e1;

   The following images show how the compiler will allocate memory for the employee structure.

The memory for the employee structure is allocated as

    int id; <------ 4 bytes
    
    char name[10] <-----10bytes
     
    int b; <------4 bytes
    
    The total memory allocated for the employee structure is 18(4+10+4) bytes.

    1 Byte=8bits.

    The Memory is allocated in terms of bytes.

Accessing Members of the Structure: There are two ways to access the members of the structure.

    1. By using .(dot operator) 
    
    2. By using ->(structure pointer operator)

    dot(.) Operator: The dot operator is used to access the data members of the structure using variables. It acts as an interface between variables and data members of the structure.

    Example: Let's see a simple example for accessing data members of structure using dot(.) operator.

    Program:

 

    Output:


   Explanation: The actual size of the structure is 18. But you will never get the size of the structure exactly as you think it must be. The sizeof 
operator returns the size of a structure that is bigger than it is because the compiler needs to pad structure members so that each member can be accessed faster without delay. 


 pointer(->) Operator: Also called Arrow Operator. It is used to access the data members of the structure using pointers.

    Program:


   Output:


    Explanation: In the above example, *emp is created as a structure pointer variable. After declaring the pointer variable, it is important to allocate the memory using the malloc() function. malloc(), calloc() are the dynamic memory allocation functions, which are discussed later. To Initialize and access the data members we use the pointer variable using arrow operator.  

Structures as a Function Arguments: You can pass a structure as a function argument in the same way you pass a variable or a pointer.

   Example: Program for passing structure as a function argument.

   Program:


    Output: 


   Explanation: Make sure to declare the employee structure and function before the main function. The compiler will checks whether the declaration is done or not before the main function. But we can access the members anywhere in the program using structure variables.


    In this tutorial, you have learned how to define and use C structure and also to create new data types that wrap multiple variables into one variable(structure name) to make it more efficient to manipulate data.


prev<---Functions in C                                        Next Topic--->Unions in C 
 



No comments:

Post a Comment