C& CPP Tutorials
About C
Program Structure
Running C Program
Variables, constants
Operators
Control Structure
Array
Function
Pointers
Preprocessor
Structure
File
Bitwise Operators
Recursion
 Data Structures
Introduction
Stacks
Queues
Linked List
Sorting 
Searching
 Test Your Skill
Fundamentals
Input and Output
Branching and Looping
Function
Pointers I
Pointers II
Structure and Union
Sample Problems I
Sample Problems II
 Help and Support
C Forum
Source code
C  Yahoo Group
C Compilers
 

Solutions : Structures & Unions

[Q001] 

 Ans. (d) Though both <struct type name> and <structure variables> are optional, one of the two must appear. In the above program, <structure variable> i.e. var is used. (2 decimal places or) 2-digit precision of 9.76723 is 9.77

 [Q002] 

 Ans. (d) Both <struct type name> and <structure variables> are optional. Thus the structure defined in the above program has no use and program executes in the normal way. 

[Q003] 

 Ans. (c) The members of a structure variable can be assigned initial values in much the same manner as the elements of an array. The initial values must appear in order in which they will be assigned to their corresponding strucutre members, enclosed in braces and separated by commas.

[Q004]

 Ans. (c) In the above program, values is the user-defined structure type or the new user-defined data type. Structure variables can then be defined in terms of the n ew data type.

[Q005] 

Ans. (a) C language does not permit the initialization of individual structure members within the template. The initialization must be done only in the declaration of the actual variables. The correct way to initialize the values is shown in [Q003] or [Q004].

[Q006] 

 Ans. (d) Illustrating 3 different ways of declaring the structres : first, second and third are
the user-defined structure type. s1, s2 and s3 are structure variables. Also an expression of the form ++variable.member is equivalent to ++(variable.member), i.e. ++ operator will apply to the structure member, not the entire structure variable.

[Q007] 

 Ans. (b) Since value of the member 'i' can be accessed using var.i, vptr->i and (*vptr).i Similarly 5th value of the member 'val' can be accessed using var.val[4], *(var.val+4), vptr->val[4], *(vptr->val+4), (*vptr).val[4] and *((*vptr).val+4)

[Q008] 

  Ans. (c) This program illustrates the transfer of a structure to a function by passing the structure's address (a pointer) to the function.

[Q009] 

 Ans. (b) This program illustrates the transfer of a structure to a function by value. Also the altered structure is now returned directly to the calling portion of the program.

[Q010] 

Ans. (d) This program illustrates the transfer of a structure to a function by passing the structure's address (a pointer) to the function. Also the altered structure is now returned directly to the calling portion of the program.

[Q011]
 Ans. (c) This program illustrates the implementation of a nested structure i.e. structure inside another structure.

[Q012] 

Ans. (d) An entire structure variable can be assigned to another structure variable, provided both variables have the same composition. 

[Q013] 

  Ans. (c) It is sometimes desirable to include within a structure one member i.e. a pointer to the parent structure type. Such structures are known as Self-Referencial structures. These structures are very useful in applications that involve linked data structures, such as lists and trees. [A linked data structure is not confined to some maximum number of components. Rather, the data structure can expand or contract in size as required.]

[Q014]

  Ans. (d) Since all the above structure declarations are valid in C.

[Q015] 

  Ans. (c) The above program produces erroneous output (which is machine dependent). In effect, a union creates a storage location that can be used by any one of its members at a time. When a different member is assigned a new value, the new value supercedes the previous member's value. [NOTE : The compiler allocates a piece of storage that is large enough to hold the largest variable type in the union i.e. all members share the same address.]

[Q016] 

 Ans. (b) Since int (2 bytes) + float (4 bytes) = (6 bytes) + Largest among union is int (2 bytes) is equal to (8 bytes). Also the total number of bytes the array 'temp2' requires :
(8 bytes) * (5 bytes) = (40 bytes).

 [Q017] 

Ans. (b) The four fields within 'v' require a total of 10 bits and these bits can be accomodated within the first word(16 bits). Unnamed fields can be used to control the alignment of bit fields within a word of memory. Such fields provide padding within the word. [NOTE : Some compilers order bit-fields from righ-to-left (i.e. from lower-order bits to high- order bits) within a word, whereas other compilers order the fields from left-to-right (high- order to low-order bits).

[Q018] 
Ans. (c)a=1 (1 bit: 0 or 1)
b=3 (2 bits: 00 or 01 or 10 or 11), 
c=7 (3 bits: 000 or 001 or 010 or 011 or 100 or 101 or 110 or 111)
d=15 (4 bits: 0000 or 0001 or 0010 or 0011 or 0100 or 0101 or 0110 or 0111 or 1000 
or
1001 or 1010 or 1011 or 1100 or 1101 or 1110 or 1111)
 [Q019] 
Ans. (a) Since we cannot take the address of a bit field variable i.e. Use of pointer to access the bit fields is prohibited. Also we cannot use 'scanf' function to read values into a bit field as it requires the address of a bit field variable. Also array of bit-fields are not permitted and a function cannot return a bit field.
[Q020]

Ans. (d) Here the bit field variable 'a' will be in first byte of one word, the variable 'i' will be in the second word and the bit fields 'b' and 'c' will be in the third word. The variables 'a', 'b' and 'c' would not get packed into the same word. [NOTE: one word=2 bytes]

Back Next
 

Google