7

how to assign two dimensional array to **pointer ? this is the idea of what i want to do

int arrray [2][3];
int **pointer = array;
so pointer[0][1]= 1;

so any help ? thanks in advance

2 Answers2

20

Declare the pointer like this:

int (*pointer)[3] = array;

But this is infinitely nasty in C++. Perhaps you could find a better solution (one involving vectors and whatnot) if you explained what your general purpose is.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • Agreed. STL will save a lot of headache. – chris Apr 15 '12 at 20:13
  • this is not what i want at ll , this is one dimensional array thank u for ur effort –  Apr 15 '12 at 20:16
  • @AhmedZainElDein: what you want cannot be done, and this is an approach to provide something that will be *source* compatible with what you *want*. – David Rodríguez - dribeas Apr 15 '12 at 20:18
  • @user1308378 : This _will_ do what you asked - it is a pointer to _an undefined number_ of one dimensional arrays `pointer[n][i];` is a valid. – Clifford Jan 04 '18 at 22:35
  • I agree this does work. Although I claim _infinitely nasty_ in C++ is a big claim that may be an overstatement. Sometimes we have to support legacy code originally written in C and are not given enough time to fully switch it all over. What if I claimed it was *nasty, but finitely nasty* ;) – Chip Grandits Feb 20 '18 at 19:03
7

The simple answer is that you cannot. A bidimensional array is a contiguous block of memory that holds each line, while a pointer to pointer can refer to a memory location where a pointer to a different memory location containing the integers is.

You can on the other hand create a separate data structure that holds the pointers to the elements in the way you want (i.e. create an array of pointers, initialize those pointers to the beginning of each row, and use a pointer to that array of pointers as pointer), but it is not useful at all, but rather will complicate everything unneedingly.

The question probably comes from the common misconceptions that arrays and pointers are the same, which they are not. An array can decay to a pointer to the first element of the array (and will do so quite often), but the type of that pointer is the type of the first element. In a bidimensional array, the type of the first element is the inner array, not the basic element type.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • ok thank u very much , i know that array itself is a pointer for instance arr[2] can be deal with it like *arr *(arr+1)= 3 how can i do the same thing but with two dimensional array ?? arr[3][2] , can i say **arr = 0 etc...... –  Apr 15 '12 at 20:58
  • @AhmedZainElDein: NO, you are wrong and it is a common misconception. The array is **not** a pointer to the first element they are completely different types even if there is an implicit conversion from array to a pointer to the first element. Once you really understand that they are *not* the same thing it will be easier to understand why what you are asking for cannot be done. Another way to look at it is drawing the memory layout of the bidimensional array (single block of size N*M elements) and then realizing that there is no pointer inside that block, so you cannot create a pointer to it – David Rodríguez - dribeas Apr 15 '12 at 21:15
  • thank u for enlighten me . i am more incline to what u are saying cause i can't find away to make **arr reference to arr[3][2] at all but that makes me asking another question what in one dimensional array it works int arr[2] , i can say *arr = 0 ; *(arr+1)= 3 –  Apr 15 '12 at 21:53
  • @AhmedZainElDein: Given an array of type T and name `array`, the expression `array` used as an *rvalue-expression* is converted (commonly called decay) into a pointer to the first element. That is, `array` is translated into `&array[0]` which is of type `T*`. Now, in a bidimensional array T is `U [N]` for a given type U and a size N, which means that in `int array[3][3];`, `array` decays into `&array[0]` which is of type `int (*)[3]`. The resulting expression is no longer an array, but a pointer and cannot decay further. – David Rodríguez - dribeas Apr 15 '12 at 22:06
  • ... compare with the expression `*array`, where `array` will decay into `&array[0]` yielding `*&array[0]` which is of type *array of 3 `int`* and thus will decay itself into the address of the first element: `&(*&array[0])[0]`, which is equivalent to `&array[0][0]`. This is probably confusing, but the important part is that when an array is used as an *rvalue-expression* it will be converted to the equivalent pointer, but only under that circumstance, and the converted pointer is no longer an array and cannot be converted again... – David Rodríguez - dribeas Apr 15 '12 at 22:10