MCQs
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.
An entire structure variable can be assigned to another structure variable, provided both variables have the same composition.
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]
Which of the following declaration is NOT valid?
#include<stdio.h>
(i) struct A{
int a;
struct B {
int b;
struct B *next;
}tempB;
struct A *next;
}tempA;
(ii) struct B{
int b;
struct B *next;
};
struct A{
int a;
struct B tempB;
struct A *next;
};
(iii)struct B{
int b;
}tempB;
struct {
int a;
struct B *nextB;
};
(iv) struct B {
int b;
struct B {
int b;
struct B *nextB;
}tempB;
struct B *nextB;
}tempB;
Since all the above structure declarations are valid in C.
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).
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
What will be output if you compile following c code ?
#include<stdio.h>
struct my_struct1
{
int arr[2][2];
};
typedef struct my_struct1 record;
struct my_struct2
{
record temp;
}list[2]={1,2,3,4,5,6,7,8};
void main()
{
int i,j,k;
for (i=1; i>=0; i--)
for (j=0; j=0; k--)
printf("%d",list[i].temp.arr[j][k]);
}
This program illustrates the implementation of a nested structure i.e. structure inside another structure.
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.]
What will be output if you compile following c code ?
#include<stdio.h>
typedef struct
{
int i;
float f;
}temp;
temp alter(temp *ptr,int x,float y)
{
temp tmp = *ptr;
printf("%d %.2f\n",tmp.i,tmp.f);
tmp.i=x;
tmp.f=y;
return tmp;
}
void main()
{
temp a={65535,777.777};
a = alter(&a,-1,666.666);
printf("%d %.2f\n",a.i,a.f);
}
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.
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.