What is structure? How to access the elements of structure? How to calculate size of structure? Explain with example.

Question: What is structure? How to access the elements of structure? How to calculate size of structure? Explain with example.


What is structure? How to access the elements of structure? How to calculate size of structure? Explain with example.

What is structure?

   Structure is a user-defined datatype in C language which allows us to combine data of different types together. Structure helps to construct a complex data type which is more meaningful. It is somewhat similar to an Array, but an array holds data of similar type only.

Accessing Structure Members

  • Array elements are accessed using the Subscript variable, Similarly Structure members are accessed using dot [.] operator.
  • (.) is called as “Structure member Operator”.
  • Use this Operator in between “Structure name” & “member name”

How to access the elements of structure?
Structure members are accessed using dot (.) operator.
  1. #include<stdio.h> 
  2.   
  3. struct Point 
  4.    int x, y; 
  5. }; 
  6.   
  7. int main() 
  8.    struct Point p1 = {0, 1}; 
  9.   
  10.    // Accessing members of point p1 
  11.    p1.x = 20; 
  12.    printf ("x = %d, y = %d", p1.x, p1.y); 
  13.   
  14.    return 0; 
  15. }
Output:
x = 20, y = 1
Output Screenshot:
elements of structure output screenshot

How to calculate size of structure?
   Generally, size of structure is addition of size of each member field. But compiler may add some extra bytes for padding/align members appropriately. This is general guideline may differ depending upon compiler/platform you choose. The size is at least sizeof(int) + sizeof(struct node *) + sizeof(struct node *).
  • Formula for Calculating Size of Structure:
    • Size of Structure 'S' = sizeof(roll) + sizeof(name.
    • = 2 + 10 + 2.
    • = 14.

Example:
  1. /***************************************************
  2.  Statement - Calculate Size of Structure
  3.  By            - www.info-gtu.in
  4.  ************************************************/


  5. #include<stdio.h>

  6. struct stud {
  7.     int roll;
  8.     char name[10];
  9.     int marks;
  10. };

  11. int main() {
  12.     int size;
  13.     struct stud s;
  14.     
  15.     size = sizeof(s);
  16.     printf("nSize of Structure : %d", size);
  17.     
  18.     return(0);
  19. }

  20. /*
  21.  Explanation:
  22.  ---------------
  23.  Structure is Collection of elements of the Different data Types.
  24.  Size of the Structure Can be Evaluated using “sizeof Operator”
  25.  
  26.  size = sizeof(s);
  27.  
  28.  Formula for Calculating Size of Structure:
  29.  -------------------------------------------
  30.  Size of Structure 'S' = sizeof(roll) + sizeof(name) + sizeof(mark)
  31.  = 2 + 10 + 2
  32.  = 14
  33.  ->Size depends on your computer
  34.  
  35.  Remember:
  36.  ----------
  37.  sizeof is Operator not function
  38.  sizeof Operator Takes any Variable as Parameter.
  39.  */

Output:

nSize of Structure : 20


Output Screenshot:
Calculate Size of Structure Output Screenshot

Download the Android app to get all Government Job Notifications on your Mobile.
Download Now
Important: Please always Check and Confirm the above details with the official Advertisement / Notification.
Previous Post Next Post