facebook like button

24 May, 2011

struct: use of structures in C

All of us know that we have some built-in data types and functions in C. We also know that we can also have user defined functions in C. Now question comes: "Can we have our own data-types ??". Answer is YES you can. Before that you need to have knowledge of something called structures. Just for now think structures as a way to represent complex data-types that are combination of known(built-in) data-types. For example suppose in a program you need a single variable which would be containing name and age of a person. By using only built-in data-types it is not possible. Some of you may say that we can have a character string variable which can  contain name and age separated with a <space> but still it would give headache to access both of them independently and using age as integer. For this we can use a structure variable. This is done by using struct keyword. See the below example.

struct person
{
   char name[20];
   int age;
};
This is an example of defining a structure named person. Now we can use its reference to create more variables of complex data-types which are like person in their look. This is done by following statement:

struct person person1;

Above statement creates a variable person1.
This types of structures we will be using in the upcoming programs. Just wait and watch.

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply