Structure

Question 1
Following declaration of an array of struct, assumes size of byte, short, int and long are 1,2, 3 and 4 respectively. Alignment rule stipulates that n-byte field must be located at an address divisible by n, the fields in a struct are not rearranged, padding is used to ensure alignment. All elements of array should be of same size.
Struct complex
Short s
Byte b
Long l
Int i
End complex
Complex C[10]
Assuming C is located at an address divisible by 8 , what is the total size of C, in bytes?
A
150
B
160
C
200
D
240
Question 2
What is the output of the following code ?
1main()
2struct s1
3{
4char *z;
5int i;
6struct s1 *p;
7}
8static struct s1 a[]={
9{"Nagpur",1,a+1}
10{"Raipur',2,a+2}
11{"Kanpur',3,a}
12};
13struct s1* ptr=a;
14printf(%s%s%s\n",a[0].z,ptr->z,a[2].p->z);
15}
A
Nagpur Raipur Kanpur
B
Nagpur Nagpur Nagpur
C
Kanpur Kanpur Kanpur
D
Error
Question 3
Suppose you are compiling on a machine with 1-byte chars, 2-byte shorts, 4-byte ints, and 8-byte doubles, and with alignment rules that require the address of every primitive data element to be integer multiple of the element's size. Suppose further that the compiler is not permitted to reorder fields; padding is used to ensure alignment. How much space will be consumed by the following array? struct { short s; char c; short t; char d; double r; int i; }A[10]; /*10 element array of struct*/
A
150 bytes
B
320 bytes
C
240 bytes
D
200 bytes
There are 3 questions to complete.

Access quiz wise question and answers by becoming as a solutions adda PRO SUBSCRIBER with Ad-Free content

Register Now

If you have registered and made your payment please contact solutionsadda.in@gmail.com to get access