Study C MCQ Questions and Answers on Arrays, Multidimensional Arrays and Pointers. Easily attend technical interviews after reading these Multiple Choice Questions.
Go through C Theory Notes on Arrays before studying questions.
See the number of [ ] square bracket pairs. Here there are 2 ary[10][5]. So the dimension is TWO 2.
int ary[]={1,3,5,7};
It is a Single Dimension Array. Only [] one pair of square brackets is present.
int main()
{
int ary[] = {1, 3, 5};
printf("%d %d", ary[-1], ary[4]);
return 0;
}
You are accessing -1 index and 4 index which is outside the limit 0 to 2. You get only garbage values.
int main()
{
static int ary[] = {1, 3, 5};
printf("%d %d", ary[-1], ary[5]);
return 0;
}
0 0 is the answer. Storage Class static makes all uninitialized variables to hold default ZERO values.
int main()
{
int ary[] = {11, 33, 55};
int *p, *q;
p = ary;
q = ary+2;
printf("%d %d",*p, *q);
return 0;
}
Incrementing a pointer points to the address of next element. ary points to first element. ary +1 points to second element. ary+2 points to the third element.
One square bracket = Single Dimension or One dimension.
int main()
{
int ary[3][2] = {1,2,3,4,5,6};
printf("%d %d", ary[0][0], ary[2][1]);
return 0;
}
[3] represents 3 rows. [2] represents 2 columns. So each row contains two elements. Index of row and column start with ZERO 0. So ary[2][1] represents 3rd row, 2nd element.
int main()
{
int ary[3][] = {6,5,4,3,2,1};
printf("%d %d", ary[0][0], ary[2][1]);
return 0;
}
ary[3][] has missing column count. Column count is a must for any multidimensional array.
int main()
{
int ary[][3] = {6,5,4,3,2,1};
printf("%d %d", ary[0][0], ary[1][0]);
return 0;
}
ary[][3] divides all 6 elements into two rows each containing 3 elements. 6/col = 6/3 = 2 rows. {{6,5,4}, {3,2,1}}
int ary[][3] = {6,5,4,3,2,1};
int main()
{
int ary[][2][3] = {
{{1,2,3},{4,5,6}},
{{7,8,9},{10,11,12}}
};
printf("%d %d", ary[0][0][0], ary[1][1][1]);
return 0;
}
a[][2][3] has missing first dimension. It is valid to ignore first dimensional value if you want. 1st dimension = 12/(2*3) = 12/6 = 2.
int ary[] = {1,2,3};
int bry[][2] = {{1,2},{3,4}}
int main()
{
int ary[2][2][3] = {
{{1,2,3},{4,5,6}},
{{7,8,9},{10,11,12}}
};
int *p;
p = &ary;
printf("%d %d",*p, *p+11);
return 0;
}
*p points to the first element. *p + 11 is the 12th element which is 12 in the array. It is same as ary[1][1][2].
int main()
{
int ary[2][2][3] = {
{{1,2,3},{4,5,6}},
{{7,8,9},{10,11,12}}
};
printf("%d",*(*(*(ary+1)+ 1)+2));
return 0;
}
*(*(*(ary+i)+ )+k) = ary[i][j][k]
A 4 dimensional array is a collection of 3 dimensional arrays.