Structures In C

We have seen that arrays can be used to represent a group of data items that belong to the same type, such as int or float. However, if we want to represent a collection of data items od different types using a single name, then we cannot use an array. C supports a constructed data types knows as structure, which is a method for packing data of different types. A structure is a collection of variables referenced under one name, providing a convenient means of keeping related information together. The various individual data elements in a structure can be accessed and processed separately. A structure declaration forms a template that may be used to create structure objects (that is, instances of a structure). For example, it can be used to represent a set of attributes of a student, such as roll number, name, age and marks. The concept of a structure is analogous to that of a ‘record’ in many programing languages.

Consider the following declaration of separate variables

int rollno;
char name [20];
int age;
float fees;

if we want more than one such set, we need to define more variables with a new name. But it can be declared using structure as follows. the format for structure declaration is shown in figure.

struct student                      
{                       
    int rollno;             
    char name [20];
    int age;
    float fees;
};
img

The keyword struct declares a structure to hold the details of four fields, namely rollno, name age and fees. These fields are called structure elements of members. Each member may belong to a different type of data. Student is name of structure and is called the structure tag. The tag name may be used subsequently to declare variables that have the tag’s structure.

Defining structures

The syntax of structure is as follows

struct tagname
{
   first member declaration;
   .  .  .  .
   .  .  .  .
   last member declaration;
} ;

Where tagname is optional, but needed to declare other variables with this structure on later statements. We can declare structure variables using the tag name anywhere in the program.


Giving values to members

We can assign values to the member of structure. Since the members themselves are not variables, they should be linked to the structure variables in order to make them meaningful members. The link between a member and variable is established using the member operator ‘.’ which is also known as dot operator or period operator. A structure member can be accessed by writing

variable member

where variable refers to the name of a structure type variable and member refers to the name of an element within the structures.


Initializing structures

Like any other data type of a structure variable can be initialized.

  • • Structure variable can be initialized when defined by using values of the proper type for each member inside braces
  • • Structure initialization is similar to array initialization, but value types can very depending or corresponding member types of the structures

C does not support the initialization of individual structure members within the template. The initialization must be done only in the declaration of the actual variables.



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