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;
}
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
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.
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
*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,
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