Tuesday, 13 June 2023

Structures and Unions in C

What is Structures and Unions?

Array is a collection of same type of component but in much real life appeal we may need to group mismatched types of logically related data. For example if we want to generate a record of a person that contains name, age and height of that person, then we can’t use array since all the three data elements are of different types. For example record has three name (string), age (int) and height (int).

structures, arrays, defining a structure, c language program
Structures and Unions in C Image-1

To store these connected fields of different data types we can use a structure, which is able of storing diverse data. Data of mismatched types can be grouped jointly under a single name using structures. The data component of a structure is referred to as members.

1.1   Defining a Structure

Definition of a structure generates a template or format that report the quality of its members. All the variables that would be state of this structure type, will take the form of this format. The general syntax of a structure definition is-

struct tagname{

datatype member1;

datatype member2;

……………………………

datatype memberN;

};

Here struct is a keyword, which tells the accumulator that a structure is being defined. member1, member2, ……………… memberN are known as members of the structure and are communicate inside curly braces. There should be a semicolon at the end of the wavy braces. These members can be of any data type like int, float, array, char, pointers or another structure type. tagname is the name of the structure and it is used far in the program to communicate variables of this structure type.

 

Definition of a structure comes up with one more data type in adding to the built in data types. We can state variables of this new data type that will have the format of the explain structure. It is major to note that definition of a structure format does not put aside any space in memory for the members; space is booked only when actual adaptable of this structure type are declared. Although the syntax of statement of major inside the format is identical to the syntax we use in declaring variables but these members are not variables, they don’t have any alive until they are fond of with a structure variable. The member names inside a structure should be mismatched from one another but these names can be alike to any other variable name state outside the structure. The member names of two different structures also be same.

 

Let us take an example of describe a structure format.

                struct student {

                                                char name [20];

                                                int rollno;

                                                float marks;

                                                };

Here student is the structure label and there are three members of this structure viz rollno, name and marks. Structure format can be explain globally or locally i.e. it can be put down before all functions in the program or it can be locally near in a function. If the format is global then it can be used by all task while if it is local then only the function carry it can use it.


Computer stuff kit tricks of Topics 58.