answersLogoWhite

0


Best Answer

Structures are handy if you have a certain set of types of information that will be used repeatedly. For example, a structure called 'address' might consist of the street number, the city, the zip code, the state, and the country. You could then create several instances of this structure, maybe for several employees or something. Each instance would have its own zip code, state, country variable etc.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

C structures allow a programmer to combine variables related to a concept into one block. For instance, if you wanted to declare variables relating to a vehicle, you could use the following:

char *vehicle_make, *vehicle_model;

int vehicle_year;

enum { color_red, color_blue, color_green, color_gold, color_silver } vehicle_color;

vehicle_make="Slodge";

vehicle_model="Guzzler";

vehicle_year=2010;

vehicle_color=color_gold;

However, sometimes you'll want to maintain multiple vehicles in memory at once. Using the above method, you'd have to employ something like the following block:

char *vehicle1_make, *vehicle1_model;

int vehicle1_year;

enum { color_red, color_blue, color_green, color_gold, color_silver } vehicle1_color;

char *vehicle2_make, *vehicle2_model;

int vehicle2_year;

enum { color_red, color_blue, color_green, color_gold, color_silver } vehicle2_color;

And so forth. This can get very messy. Instead, you can use the 'struct' declaration as follows:

enum vehicle_color { color_red, color_blue, color_green, color_gold, color_silver };

struct vehicle {

char *make, *model;

int year;

vehicle_color color;

};

struct vehicle vehicle1, vehicle2;

vehicle1.make="Slodge";

vehicle1.model="Guzzler";

vehicle1.year=2010;

vehicle1.color=color_gold;

Now that you have a struct, you can also write a function that automatically sets a vehicle's members in one statement:

void vehicle_set(struct vehicle *vehicle, char *make, char *model, int year, vehicle_color color)

{

vehicle->make=make;

vehicle->model=model;

vehicle->year=year;

vehicle->color=color;

}

vehicle_set(&vehicle1, "Slodge", "Guzzler", 2010, color_gold);

Keep in mind that if you use pointers in structures like I did above, and you begin working with files, then writing vehicle1 to a file instead of its individual members means you'll write the pointers to the variables, and not the variable data, to files.

See the related links below for more information on C in tutorial format.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why you use structure in c language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is structure and union?

Structure is a query that is used in language c++


How do you create modules in c language?

by using structure in c.........


Why is c called a structured language?

a synonym for structure language in "Comb-sturctured" languages. C in comb.


Why you are use to c language?

I do use am a programmer, because C-language.


What data structure is used to represent graphs in c language?

A tree.


What is the use of loader in c language?

There is no part called 'loader' in C language.


What is the program language c?

C is a pop language. C is a case sensetive language. C is motherof all language. C is block structure language. C is a high level language. C is advace of B language. C developed by D.richties in 1972 at AT & T Bell lab in USA. Sachin Bhardwaj 986854722 skbmca@gmail.com


How do you write a programme in c language using arrays to copy diagonal elements of a 2-d array into a 1-d array?

TO use a c language first step is to know about the c language and the steps to use the c progrmming language with the help of any elders or with the teachers. TO use the arrays you have to get th eknowledge of "c" language


What is basic structure of file handling in c language?

open, read/write, close


What is the difference between c and c plus plus extension?

c language is the structure oriented language and c does not follows the object oriented paradigms . c++ obeys the all object oriented language characteristics ========== C++ is a set of extensions to the C language to allow some (not all) principles of object-oriented programming to be used. Originally, C++ was a front end pre-processor for C and C++ compilers will translate C language functions.


What the difference between c and c plus plus?

The fundamental difference is that in C++ object-oriented programming (OOP) was added. C is a procedural language (that means. top-down structure design), where as C++, which is an extension of C itself, is an object oriented language.


Which compiler you must use to run C language?

To run C language?! Your question makes no sense.