O'Reilly logo

Express Learning - Computer Fundamentals and Programming by ITL ESL, Ashok Kamthane

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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("\n Book Name : %s",bk1.book);
printf("\n No. of Pages : %d",bk1.pages);
printf("\n 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("\n Age in years : %d",amit.age);
printf("\n Weight in kg : %f",amit.weight);
printf("\n Memory location for weight (float) : %u",&(amit.
weight));
printf("\n 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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required