int** a = new int*[col]; // works
int** b = new int[][col]; //error cannot convert from 'int (*)[1]' to 'int **
int* c[] = new int[][col]; /error cannot convert from 'int (*)[1]' to 'int *[]'

The first one works, but i get these errors when i use the other 2 forms. Can someone explain these to me. What is wrong in 2, 3 and why is 1 ok !!!

VernonDozier commented: Great question. +18

Well this is what I understand :

(LHS=Left Hand Side,similarly RHS=Right Hand Side)

1>

int** a = new int*[col];

Here the LHS tells us that a is a 2d pointer variable i.e a pointer to a pointer type.
In RHS we ask the compiler to allocate memory for "col" number of int* type pointers and return the starting address of that block allocated.
As RHS is returning a pointer to a block of int* type pointers a takes up the value as it is int** type.Hence this is correct.

2>

int** b = new int[][col];

Here the LHS is same as the first case.
Here in RHS we are asking the compiler to allocate memory for "col" number of elements to each one of the array pointers shown by int[] where we are assuming that such an array of pointers already exists which conflicts with the actual case.Hence to avoid the confusion the error.The same error persists even if you try something like this with defined limits as:

int ** b = new int[10][col];

3>

int* c[] = new int[][col];

Here the RHS is same but the LHS requires the compiler to return a pointer to the memory allocated for an array of int* type pointers.Hence the conflict which results in an error.

Basically what we clearly need to understand is that we need the array elements to be in contiguous memory locations and arrays are not just contiguous memory locations whose starting element's address is returned as the pointer to the array.It is treated as a unit of memory by the OS with its limits defined in memory locations soon after the memory allocated for array elements.

I request everyone to comment and improvise the thread reply as its a bit confusing to explain this topic and I have tried my best.

commented: Nice explanation. +18

smithss,

Pointer/array misconception.
Declaration,

int** a;

is called pointer to pointers.
How many int pointers you want?
>3

a=new int *[3];  // 3 int pointers

Now you want to allocate more memory for elements

a[0]=new int[4];  // array of 4 int elements
  a[1]=new int[2];  // array of 2 int elements 
  ....

For the following declaration - I say it is improper syntax.

int** b = new int[][col]; //error cannot convert from 'int (*)[1]' to 'int **
int* c[] = new int[][col]; /error cannot convert from 'int (*)[1]' to 'int *[]'

Following code snippet might help you to understand the basics of pointer & array.

A pointer to an array of 10 integer

int m[10]={10,20,30,40,50,60,70,80,90,100};
  int (*p)[10]=&m;
  cout << "\nSize of : " << sizeof(p); // 4 bytes
  cout << "\n" << (*p)[2];             // 30 value

An array of 10 int pointers

int *q[10];
  cout << "\nSize of : " << sizeof(q); // 40 bytes

An array of 10 int pointers - same as above.

int(*r[10]);
  cout << "\nSize of : " << sizeof(r); // 40 bytes

Pointer of (unknown quantities) int pointers

int **z;

Pointer to an array of 3 pointer of (unknown quantities) int pointers

int (**y)[3];
commented: Nice explanation. +18

An array of 10 int pointers - same as above.

int(*r[10]);
cout << "\nSize of : " << sizeof(r); // 40 bytes

To the OP:
Please note that if the code looked like:

double(*r[10]); // <-- change made here
cout << "\nSize of : " << sizeof(r); // 40 bytes

would give the same output.
(This is because we're talking about pointers, and as you know: pointers hold memory addresses)

Note:: This post (and adatapost's post) assumed that integers are 4 bytes, but this can differ on your implementation.

I tried my hand commenting on this thread earlier, but after reading my own post, I decided that my comments just made things more confusing, so I didn't post. So my contribution to this thread will be to link a previous thread that I started when I wanted to get contiguous memory on multidimensional dynamic arrays. This thread had some good responses and the material is fairly related, so here it is:

http://www.daniweb.com/forums/thread127068.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.