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?
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?
150 | |
160 | |
200 | |
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}
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}
Nagpur Raipur Kanpur
| |
Nagpur Nagpur Nagpur
| |
Kanpur Kanpur Kanpur
| |
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*/
150 bytes | |
320 bytes | |
240 bytes | |
200 bytes |