structure variations In C

C structure uses the array as its member. We have already use arrays of characters inside a structure i.e., char name [20] in student structure. For example,

struct student 
{
   int rollno; 
   char name [20];
   int mark [3];
} student1;

Here, the member mark contains three elements

student1. mark [0]
student1. mark [1]
student1. mark [2]


Structure can be used as a member of another structure (nesting of structure)

Structures within a structure means nesting of structures. It means that a structure can also be used as a member field of another structure.


Arrays of structures

In our student structure, we have declared three variables student1, student2 and student3 to record only three student’s information. If we want to record many student’ s information, we have to declare array of structures.


Pointers to structures

The beginning address of a structure can be accessed in the same manner as any other address, through the use of the address (&) operator. Thus, if variable represent a structure – type variable, then &variable represents the starting address of that variable. we can declare a pointer variable for a structure by writing

type *ptr;

where type is a data type that identifies the composition of the structure, and ptr represents the name of the pointer variable. We can assign the beginning address of a structure variable to this pointer by writing

ptr = &variable;

Pointer to structure can be declared as struct type with *indirection operator

struct cbox
{
    double length;
    double width;
    double height;
    char *color;
} blk1, blk2;

struct cbox *anybox;        /*anybox is a pointerto cbox */

  • • Address of structure can be assigned to a pointer with & operator
  • anybox = &blk1;

  • • Structure member via pointer is accessed with “->” pointer or “*.”


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext