what are the advantages of using structure in c


What are the advantages of using structure in C Program

Declaring a struct is a two-stage process. The first stage defines a new data type that has the required structure which can then be used to declare as many variables with the same structure as required. For example, suppose we need to store a name, age and salary as a single structure. You would first define the new data type using:

struct emprec

{

char name[25];

int age;

int pay;

};

and then you would declare a new variable:

struct emprec employee

Notice that the new variable is called employee and it is of type emprec which has been defined earlier.

To get the individual components of a structure you have to use qualified names. i.e. You first give the name of the structure variable and then the name of the component separated by a dot. For example, given:

struct emprec employee

then: employee.age is an int and:

employee.name

is  a  char  array.  Once  you  have  used  a  qualified  name  to  get  down  to  the  level  of  a component then it behaves like a normal variable of the type. For example: employee.age=32;

is a valid assignment to an int

As we have seen, a structure is a good way of storing related data together. It is also a good way of representing certain types of information. Complex numbers in mathematics inhabit a two dimensional plane (stretching in real and imaginary directions). These couldeasily be represented here by

struct {

double real;

double imag;

} complex;

In a similar way, structures could be used to hold the locations of points in multi- dimensional space. Mathematicians and engineers might see a storage efficient implementation for sparse arrays here.

Apart from holding data, structures can be used as members of other structures. Arrays of structures are possible, and are a good way of storing lists of data with regular fields, such as databases.

Another possibility is a structure whose fields include pointers to its own type. These can be used to build chains (programmers call these linked lists), trees or other connected structures.

 

Request for Solution File

Ask an Expert for Answer!!
Computer Engineering: what are the advantages of using structure in c
Reference No:- TGS0305520

Expected delivery within 24 Hours