5

When I try to compile the following code with g++:

int main()
{
    int a[3] = {0, 1, 2};
    int (* p)[] = &a;
}

compiler gives the following error : "cannot convert ‘int ()[3]’ to ‘int ()[]’ in initialization". Why isn't it possible to convert int ()[3] to int ()[]? And if it is not possible then how a variable of type 'int (*)[]' should be initialized?

Hrant
  • 496
  • 1
  • 5
  • 13

3 Answers3

9

Because you have to specify the length of the array your pointer pints to. It should be like this:

int (* p)[3] = &a;

int (*p)[] this means that your p is a pointer to an array. The problem is the compiler has to know at compile time how long is the array that pointers points to, so you have to specify a value in the brackets -> int (*p)[x] where x is known at compile time.

Alexandru Barbarosie
  • 2,952
  • 3
  • 24
  • 46
  • Then there is no way to initialize int (* p)[] – Hrant Jul 02 '13 at 18:40
  • No, there is no way to initialize it, because such a type is meaningless in C++. – Carl Norum Jul 02 '13 at 18:51
  • @CarlNorum the type `int (*)[]` isn't meaningless. It's a pointer to an array of unknown size. dereferencing it produces an lvalue of type array of unknown size. It's even possible to access the array's elements, because the individual element size is known even if the size of the whole array isn't. – bames53 Jul 02 '13 at 19:08
  • You're right - I didn't think you could even create such a variable, but a quick test here proved me wrong. – Carl Norum Jul 02 '13 at 19:56
4

There are many ways to initialize a variable of type int (*)[]. For example, it can be initialized by other values of int (*)[] type (including ones produced by an explicit cast) or by null-pointer constant. An int (*)[3] value will not immediately work since it has a different type.

In your case I'd expect this to be perfectly legal and defined

 int (*p)[] = (int (*)[]) &a;

with further access to the afray as (*p)[i].

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

You can probably do what you want (like access each element with p[0], p[1], and p[2]) with this:

int *p = a;

Unless you really want a pointer to a pointer, in which case use:

int **p = &a;
wallyk
  • 56,922
  • 16
  • 83
  • 148
  • 2
    This is just a pointer to the first element, not the entire array. – Hunter McMillen Jul 02 '13 at 18:35
  • 3
    @HunterMcMillen: A pointer to the first element *is* a pointer to the array. – wallyk Jul 02 '13 at 18:36
  • 2
    @wallyk: I don't think so -- see http://stackoverflow.com/questions/5956493/address-of-an-array/17413762#17413762 – Nate Kohl Jul 02 '13 at 18:42
  • 1
    C++ guarantees that array allocations are contiguous, so yes a pointer to the head is a pointer to the array. – Joel Jul 02 '13 at 18:44
  • 1
    The second declaration is completely incorrect. You cannot do `int **p = &a`. `&a` has type `int (*)[3]`, not `int **`. Forcing such value into a `int **` variable makes no sense. – AnT stands with Russia Jul 02 '13 at 18:46
  • 1
    please have a look at this: http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c – Hunter McMillen Jul 02 '13 at 18:58
  • Using a pointer instead of a pointer to an array is the typical thing to do. Arrays and pointers are not the same, however, so a pointer to a pointer and a pointer to an array are completely different, incompatible objects. – bames53 Jul 02 '13 at 19:11
  • 1
    I sit corrected. Great reference, Hunter. Basically they will always have the same value, and you can access all the elements, but they are different types according to C++'s type system. – Joel Jul 02 '13 at 19:12