1,225 views

2 Answers

Best answer
5 votes
5 votes

Not explaining for options C and D as they are wrong from the definition of function and structures. 

We have an array of pointers. For simplicity let the pointers be to an int. So, this is like

int * array[100];

The above one becomes an array of pointers because the precedence of array operator is higher than that of *. Otherwise we would have had to write like

int * (array[100]);

Now, this is not the same as a pointer to array. Because if 'p' is a pointer to an array *p should give an array. Here, *array = *(array+0) = array[0] = pointer to an integer which is not an array. Being a pointer we can use array[i][j] and this works like an array, but is not the same when we do sizeof(array[0]) which gives size of pointer and not the size of array being pointed.

Now, array of pointers is also not the same as pointer to a pointer. Again size is the villain here. For a pointer to a pointer (say int **p), sizeof(p) will be size of pointer but for an array of pointers, sizeof will return n*sizeof pointer where n is the array size.

So, none of the options are correct. So, lets try choosing the possible answer (getting to the mind of the question asker). In C/C++, when an array is passed to a function, it is done by passing the base address and in the function the array behaves like a pointer. So, an array of pointers when passed to a function becomes a pointer to pointer which is option B. The below code shows the difference between the two. 

#include<stdio.h>
int array(int a[100])//this is same as int *a
{
        printf("size of array in function is %u\n", sizeof a);
}
int main()
{
        int a[100];
        printf("size of array in main is %u\n", sizeof a);
        array(a);
}
Output:
size of array in main is 400
size of array in function is 8

The above output is on a machine where sizeof(int) = 4, and sizeof pointer is 8. 

selected by
0 votes
0 votes
NoanswerwouldbeONLY(A)

Related questions

0 votes
0 votes
1 answer
2
mohit chawla asked Sep 18, 2016
538 views
Please help me out guys