The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+4 votes
354 views
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 by Boss (32.1k points) | 354 views
0
I thunk option A and B both should be the answer

2 Answers

+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. 

by Veteran (418k points)
selected by
0 votes
NoanswerwouldbeONLY(A)
by Active (3.7k points)
0

@ 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.

+1
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

Related questions

0 votes
1 answer
3
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,984 questions
55,135 answers
190,487 comments
85,105 users