Sunday, 20 May 2012

What is difference between structure and union? Explain the use of typedef statement


          Structure is the collection of different data types. It allocates memory for the highest data items. We can access more than one element at a time of all items. We can access the elements of structure by using dot (.) operator in case of normal variable or by using (→) arrow operator in case of pointer.
Unions are concept borrowed from structures and therefore follow the same syntax as structures. However, there is a major distinction between them in terms of storage. In structures, each member has its own storage location; where as all the members of a union uses the same location. This implies that, although a union may contain many members of different types, it can handle only one member at a time. Like structure, a union can be declared using the keyword union as follows:
union item
{
   int m;
   float x;
   char c;
}code;
This declare a variable code of type union item
Typedef statement is only used for making user define keywords. For example,
main()
{
typedef int amit;
amit num;
printf("enter a number ");
scanf("%d",num);
printf("your entered number is %d",num);
return 0;
}
Here, amit become an integer keyword.

No comments:

Post a Comment