Array and Pointer C Programming Mock Test – 3

Question 1
What is the output of the following program?
Consider the starting address of int array a is 1000 and b is 2000.
Assume size of int data type is 4 Byte.
#include < stdio.h > 
int main()
{
     int a[10], b[3][5];
     printf(" %d  %d ", a, b) ;
     printf("%d  %d", a+2,b+2) ;
     return 0;
}
A
1000 2000 1002 2002
B
1000 2000 1008 2008
C
1000 2000 1008 2040
D
1000 2000 1008 2024
   C Programming
Question 1 Explanation: 
Increment of 'a' is by 4-bytes, sizeof(int), but the increment of 'b' is by 20-bytes. The question is why?
The answer lies in the row-major memory space allocation of 1-D and 2-D array by the C compiler.



b is the starting address of the 0th row.
b+1 is the starting address of the 1st row.
b+i is the starting address of the ith row.

The size of a row is c * sizeof(int) = 5 * sizeof(int) = 5 * 4 = 20 bytes
where c is the number of columns

'b' is a pointer constant of type int [][5], a row of five int. If such a pointer is incremented, it goes up by 5 * sizeof(int) (number of bytes).
Type int [][5] is equivalent to int (*)[5]
OUTPUT : 1000 2000 1008 2040
Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE

(For further reading in depth Click Here
Question 2
What is the output of the following program?
Consider the starting address of int array b is 1000.
Assume size of int data type is 4 Byte.
#include < stdio.h > 
int main()
{
     int b[3][5]={{0,1,2,3,4},
{5,6,7,8,9},
{10,11,12,13,14}
}; ;
printf("%d %d ", b, *b) ;
printf("%d %d ", b+1, *b+1) ;
printf("%d %d ",b+2, *(b+2), *(b+2)+3) ;
     return 0;
}
A
1000 0 1004 5 1008 10 13
B
1000 0 1020 5 1040 1013
C
1000 100 1004 1004 1008 1008 1012
D
1000 1000 1020 1004 1040 1040 1052
   C Programming
Question 2 Explanation: 


If b is the address of the 0th row, *b is the 0th row itself. A row may be viewed as an 1-D array, so *b is the address of the 0th element of the 0th row.

Similarly b+i is the address of the ith row, *(b+i) is the ith row, so *(b+i) is the address of the 0th element of the ith row.

If *b is the address of the 0th element of the 0th row, *b + 1 is the address of the 1st element of the 0th row.

Similarly *b + j is the address of the jth element of the 0th row.

The difference between b + 1 and b is 20 (bytes) but the difference between *b + 1 and *b is the sizeof(int), 4 (bytes).

If *(b+i) is the address of the 0th element of the ith row, *(b+i) + 1 is the address of the 1st element of the ith row.

Similarly *(b+i) + j is the address of the jth element of the ith row.

The difference between b + i and b is 20i (bytes), but the difference between *(b + i) + j and *(b+i) is 4j (bytes).
OUTPUT : 1000 1000 1020 1004 1040 1040 1052
Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE

(For further reading in depth Click Here
Question 3
What is the output of the following program?
Consider the starting address of int array b is 1000.
Assume size of int data type is 4 Byte.
#include < stdio.h > 
int main()
{
     int b[3][5] = {{0,1,2,3,4},
{5,6,7,8,9},
{10,11,12,13,14}
};
printf("%d ", b[2][3]);
printf("%d ", (*(b+2))[3]);
printf("%d ", *(b[2]+3));
printf("%d ", *(b+2)+3);
printf("%d ", b[2]+3);  
return 0;
}
A
13 13 13 13 13
B
13 13 13 1052 1012
C
13 1052 13 1052 1052
D
13 13 13 1052 1052
   C Programming
Question 3 Explanation: 
We know that


b is the address of the 0th row,
b+i is the address of the ith row,
*(b+i) is the address of the 0th element of the ith row,
*(b+i)+j is the address of the jth element of the ith row,

We know that
*(b+i)+j is the address of the jth element of the ith row,
b[i][j] is the jth element of the ith row,
&b[i][j] is the address of the jth element of the ith row, so

*(b + i) + j is equivalent to &b[i][j]

We know that *(b+i)+j is the address of the jth element of the ith row, so

*(*(b + i) + j) is equivalent to b[i][j]

*(*(b + i) + j) is equivalent to b[i][j]
*(b + i) + j is equivalent to &b[i][j]
*(b[i] + j) is equivalent to b[i][j]
b[i] + j is equivalent to &b[i][j]
(*(b+i))[j] is equivalent to b[i][j]

b[2][3] is int value stored at row-2 and col-3 = 13
(*(b+2))[3] is equivalent to *(*(b+2)+3) is equivalent to b[2][3]=13
*(b[2]+3) is equivalent to *(*(b+2)+3) is equivalent to b[2][3]=13
For *(b+2)+3,
*(b+2) is the address of the 0th element of the 2nd row =1000+2*5*4=1040 and *(b+2)+3 is address of 3rd element of tht row =1040+3*4=1052.
b[2]+3 is equivalent to *(b+2)+3 = 1052
OUTPUT : 13 13 13 1052 1052
Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE

(For further reading in depth Click Here
Question 4
What is the output of the following program?
Consider the starting address of int array b is 1000.
Assume size of int data type is 4 Byte.
#include < stdio.h > 
int main()
{
     int b[3][5] = {{0,1,2,3,4},
{5,6,7,8,9},
{10,11,12,13,14}
};
printf("%d ", *b+1);
printf("%d ", *(b+1)+1);
printf("%d ", *(*(b+1)+1));
return 0;
}
A
1020 1024 1024
B
1004 1008 1008
C
1024 1024 6
D
1004 1024 6
   C Programming
Question 4 Explanation: 
*b is the address of the 0th element of the 0th row is 1000. So, *b + 1 is the address of the 1st element of the 0th row is 1004

b is the starting address of the 0th row (=1000),
b+1 is the starting address of the 1st row (1020),
*(b+1) is the starting address of the 0th element of the 1st row (1020),
*(b+1)+1 is the address of the 1st element of the 1st row=1024,

*(*(b+1)+1) is equivalent to b[1][1]= 6
OUTPUT : 1004 1024 6
Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE

(For further reading in depth Click Here
Question 5
What is the output of the following program?
Consider the starting address of int array b is 1000.
Assume size of int data type is 4 Byte.
#include < stdio.h > 
int main()
{
     int b[3][5] = {{0,1,2,3,4},
{5,6,7,8,9},
{10,11,12,13,14}
};
printf("%d ", *(b+2)-*(b+1));
printf("%d ", (b+2)-(b+1));
return 0;
}
A
1 1
B
20 20
C
5 1
D
20 4
   C Programming
Question 5 Explanation: 


For *(b+2)-*(b+1)
b is the starting address of the 0th row (=1000),
b+1 is the starting address of the 1st row (1020),
*(b+1) is the starting address of the 0th element of the 1st row (1020),
b+2 is the starting address of the 2nd row (1040),
*(b+2) is the starting address of the 0th element of the 2nd row (1040),
Pointer arithmetic is computed as (1040-1020)/sizeof(int)=20/4=5
NOTE: *(b+1) and *(b+2) is pointer to int element. Hence, we have divided the pointer subtraction with sizeof(int).

For (b+2)-(b+1)
b is the starting address of the 0th row (=1000),
b+1 is the starting address of the 1st row (1020),
b+2 is the starting address of the 2nd row (1040),
Pointer arithmetic is computed as (1040-1020)/(5*sizeof(int))=20/20=1
NOTE: b is a pointer constant of type int [][5], a row of five int. If such a pointer is incremented, it goes up by 5* sizeof(int) (number of bytes). Type int [][5] is equivalent to int (*)[5].
OUTPUT : 5 1
Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE

(For further reading in depth Click Here
There are 5 questions to complete.

Leave a Comment