in Programming in C
1,249 views
4 votes
4 votes
In C/C++ an array of pointers is same as

(A) Pointer to array

(B) Pointer to pointer

(C) Pointer to function

(D) Pointer to structure
in Programming in C
by
1.2k views

1 comment

I thunk option A and B both should be the answer
0
0

2 Answers

5 votes
5 votes
Best answer

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
by
0 votes
0 votes
NoanswerwouldbeONLY(A)

2 Comments

edited by

@ LavTheRawkstar

we can use array name as a pointer itself then why not option B ...

if your are answering the question then please specify the reason of your answer.   downvoting my answer does not make you over smart.....which you are trying to be.

0
0
Answer can be pointer to pointer only that too in some cases

we know pointer is a variable that stores address of another variable and array is used as mnemonic for addresses so

array of pointers means it is array of addresses and those address will point to some variable

but pointer to array is a single pointer storing base address of the array so (a) can't be answer
1
1

Related questions

0 votes
0 votes
1 answer
2