Week 12 Assignment Solution
Week 12 Assignment Solution
a) String
b) Structure
c) Char
d) All of the mentioned
Solution: (c) The scope of the member name is confined to the particular structure, within
which it is defined
++p;
while(*p != 'e')
printf("%c", *p++);
return 0;
}
a) abcd
b) bcd
c) cd
d) abcdfgh
Solution: (b)
First, the pointer points to the base of A. Then it's incremented by one to point to the element
b. While p is not pointing e, the loop keeps prints the elements one after another. Therefore,
the final answer is bcd.
7. What is the output of the following C code? Assume that the address of x is 2000 (in
decimal) and an integer requires four bytes of memory.
int main()
{
unsigned int x[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
printf("%u,%u, %u", x+3, *(x+3),*(x+2)+3);
return 0;
}
Solution: (a)
All these points to the same element. Therefore, the same value will be printed. This is an
example of pointer arithmetic in an 2D array.
a) Yes
b) No
c) Only as an array
d) Only if the structure is anonymous
Answer: a) Yes
Explanation: A structure can contain a pointer to its own type, which is often used to create
linked data structures like linked lists.
struct Point {
int x;
int y;
};
struct Point *arr[2];
struct Point p1 = {1, 2}, p2 = {3, 4};
arr[0] = &p1;
arr[1] = &p2;
printf("%d", arr[1]->y);
a) 1
b) 2
c) 3
d) 4
Answer: d) 4
Explanation: arr[1] points to p2, and arr[1]->y accesses the y member of p2, which is 4.
int main()
{
struct p p1[] = {1, 90, 62, 33, 3, 34};
struct p *ptr1 = p1;
int x = (sizeof(p1) / 3);
if (x == sizeof(int) + sizeof(char))
printf("True");
else
printf("False");
return 0;
Week 12 Assignment Solution
a) True
b) False
c) No output
d) Compilation error
Solution: (b) Size of the structure is the maximum size of the variable inside structure. Thus,
the size of each element of structure p is 4 bytes (in gcc compiler, it can vary based on
compiler). Thus, sizeof(p1) is 6*4=24. x will be 24/3=8. In the next step,
sizeof(int)+sizeof(char) is 5 which is not equal to x. Hence, false will be printed.