Structure and Union
1. What is a structure in C? 
Ans: A structure is a collection of same or different data types, grouped together under a single
structure name. It is a derived data type arranged in a group of related data items of different data types.
2. How is a structure declared?
Ans: Structure is declared as follows:
struct struct_name
{
data type 1 variable1;
data type 2 variable2;
------------------
------------------
};
Explanati on: Structure declaration always starts with struct keyword. Here, struct_name
is known as tag. The struct declaration is enclosed with a pair of curly braces. Closing brace is
terminated with a semicolon. After closing brace and before structure termination, we can provide
object names as optional for accessing the structure elements. Using struct and tag, a user
can declare structure elements such as variable1 of data type1, variable 2 of data
type 2, etc. These are the structure members. They are declared in the structure within curly
braces.
3. What is the use of the keyword struct?
Ans: The struct is a keyword in C. It is used to declare a structure of various data types/elements
followed by a tag name of that structure.
13
M13_ITL-ESL4791_02_SE_C13.indd 269 12/22/2012 5:06:14 PM
II-270 Programming Concepts
4. How to declare structure variables?
Ans: The structure variables can be declared by including variable names between the closing brace and
the terminating semicolon in the structure definition. This can be followed from the following example.
struct book
{
char titile[20];
int pages;
int cost;
}
bk1,bk2;
In the above example, the bk1 and bk2 are the variables of type struct book.
Alternately, one can also declare the structure variables as per the following example.
struct book
{
char titile[20];
int pages;
int cost;
};
struct book bk1,bk2;
In the above example bk1,bk2 are the structure variables of type struct book.
5. When is memory allocated to a structure members?
Ans: Memory is not allocated/reserved when the structure is defined. Structure definition describes
the template. Memory is allocated only after declaration of variables of the corresponding type.
For example, in the following example, memory is not reserved when structure is declared.
struct book1
{
char book[30];
int pages ;
float price ;
};
After declaring, the variables of structure memory are allocated.
After writing the above code, one can write the following code:
struct book1 bk1;
The bk1 is the variable and now memory is allocated to the members of structure book1.
6. Explain the use of the dot operator.
Ans: The dot (.) operator accesses the structure members.
For example, structure_variable.member or bk1.book
M13_ITL-ESL4791_02_SE_C13.indd 270 12/22/2012 5:06:14 PM
Structure and Union II-271
7. Write a program to access members of a structure using dot operator.
void main()
{
struct book1
{
char book[30];
int pages;
float price;
};
struct book1 bk1={"Programming in C",600,185};
clrscr();
printf(" Book Name : %s",bk1.book);
printf(" No. of Pages : %d",bk1.pages);
printf(" Book Price : %.2f",bk1.price);
getche();
}
OUTPUT:
Book Name : Programming in C
No. of Pages : 600
Book Price : 185.00
Explanation:In the above program, the struct book1 is defined with its member variables char
book [30], int pages and float price. The bk1 is an object of the struct book1.
The statement struct book1 bk1={"Programming in C" 300,285}; defines object bk1
and initializes the variables with the values enclosed in the curly braces, respectively. Using printf()
statement and dot operators, members are accessed and they are displayed.
8. How are structure elements stored in memory? Give a programming example.
Ans: The structure elements are stored in contiguous memory locations.
void main()
{
struct student
{
int age;
float weight;
};
M13_ITL-ESL4791_02_SE_C13.indd 271 12/22/2012 5:06:14 PM
II-272 Programming Concepts
struct student amit={21,45.5};
clrscr();
printf(" Age in years : %d",amit.age);
printf(" Weight in kg : %f",amit.weight);
printf(" Memory location for weight (float) : %u",&(amit.
weight));
printf(" Memory location for Age(integer) : %u",&(amit.
age));
getche();
}
OUTPUT:
Age in years : 21
Weight in kg : 45.500000
Memory location for weight (float): 65490
Memory location for Age(integer) : 65488
Explanation: Memory for float and int require 4 and 2 bytes, respectively. Program’s output together
with memory locations for storing elements is indicated at the output.
9. Explain nested structure. Draw a diagram to explain a nested structure.
Ans: We can also take objects of one structure as a member in another structure, to create complex
data applications. The syntax of the nested structure is as follows:
struct time
{
int second;
int minute;
int hour;
};
struct t
{
int carno;
struct time st;
struct time et;
};
struct t player;
M13_ITL-ESL4791_02_SE_C13.indd 272 12/22/2012 5:06:14 PM
Structure and Union II-273
Following diagram depicts the view of nested structure.
Player
Car no.
st
et
Second
Minute
Hour
Second
Minute
Hour
10. How  are  arrays  of  structure  variables  defined?  How  are  they  beneficial  to  the 
programmers?
Ans: As we know, an array is a collection of similar data types. In the same way, in array of struc-
tures, every element is of structure type. Array of structure can be declared as follow:
struct time
{
int second;
int minute;
int hour;
} t[3];
In the above example, t[3] is an array of three elements containing three objects of time structure.
They are more useful for database applications where a large data is to be stored.
11. How are structure elements accessed using pointer? Which operator is used?
Ans: To access the structure elements using pointer, an arrow operator (− and > together) is used
instead of dot (.) operator. The syntax for using pointer(ptr) with member(data_member) is as follows:
ptr->data_member.
12. Is it possible to a pass structure variable to function? Explain in detail the possible ways.
Ans: Yes, structure variables can be passed to the function by value or address. The syntax of the
same is as under.
M13_ITL-ESL4791_02_SE_C13.indd 273 12/22/2012 5:06:15 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset