0

I am trying to create an array of two 2D arrays as follows

Given

char twoDArr[2][3] =  {{'a','b','c'},  {'d','e','f'}};

you could do

char (*twoDArrP)[3] = twoDArr;

which yields valid results in:

cout << "twoDArr: " << twoDArr[1][2] << endl;
cout << "twoDArrP: " << twoDArrP[1][2] << endl;

My goal is to create another (static) array and/or pointer as follows

char (**threeDArr)[3] = {twoDArr, twoDArr}; //doesn't compile 

so that I can be able to access its values as:

char val = threeDArr[0][1][2];

Obviously the indices will vary

The question is what is the correct way of declaring the 3D array, i.e. the array of 2D arrays ??

I have searched for an example using this case and I cannot seem to find anything. Any help would be appreciated.

Gerti Tuzi
  • 85
  • 2
  • 8
  • When you say, doesn't compile, what's the actual error? – Florin Stingaciu Aug 09 '12 at 13:56
  • Your array has 3 members but you initialize it with only 2 members – giorashc Aug 09 '12 at 13:59
  • `char (**threeDArr)[3] = {twoDArr, twoDArr, 0};` does work? – cybertextron Aug 09 '12 at 13:59
  • Define "work"! It possibly creates an array of three pointers, but they don't point to anything useful. – Bo Persson Aug 09 '12 at 14:08
  • Errors I get with the code above: error C2440: 'initializing' : cannot convert from 'char (*)[3]' to 'char (**)[3]'. Using char (*threeDArr)[3] = {twoDArrP, twoDArrP}; - I get error C2078: too many initializers. I want to be able to have a way to initialize it as given: {twoDArrP, twoDArrP}; Maybe the declaration is not correct , but I want to have the lookup tables as such. Using char (**threeDArr)[3] = {twoDArrP, twoDArrP, 0}; doesn't compile either. The error I get in this case is : cannot convert from 'char (*)[3]' to 'char (**)[3]'. Sorry for the poor formatting here at the comments – Gerti Tuzi Aug 09 '12 at 14:08
  • The 3 elements seem to be referring to the 3 elements in the elementary items, that is a,b,c. – Gerti Tuzi Aug 09 '12 at 14:13
  • If I consider "work" as the goal, let's say at the end val == f. I have used the same 2D array for simplicity. In actuality there will be two different 2D arrays that will be placed in a 3D lookup table. – Gerti Tuzi Aug 09 '12 at 14:16
  • possible duplicate of [Is 2d array a double pointer?](http://stackoverflow.com/questions/7586702/is-2d-array-a-double-pointer) – Bo Persson Aug 09 '12 at 14:20
  • possible duplicate of [How do I use arrays in C++?](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) – fredoverflow Aug 09 '12 at 14:32
  • @user1587730 : you can edit your question and append the error messages (instead of putting them in a comment). – egrunin Aug 09 '12 at 14:55
  • Try : `char (*threeAddr)[x][y]` , and then you can do `threeDArr[0][1][2];` make sure you assign it like : `threeAddr = & some2DArray` – Mr.Anubis Aug 09 '12 at 16:03

2 Answers2

1

An array of

char (*twoDArrP)[3]

of size 2 can be defined as:

char (*threeDArrP[2])[3]

which is not so clear (I'm not even sure if it's entirely correct). See cdecl.

typedef is your friend:

typedef char (*twoDArrP_type)[3];

twoDArrP_type threeDArrP[2] = {twoDArr, twoDArr};

This:

char (**threeDArr)[3]

as you have written, is a pointer to a pointer to an array. Not a pointer to a 2D array.

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
  • First, I'm not claiming that the way it is declared is correct. I am trying to find the way to declare it correctly. I am at a loss myself as to the correct declaration. The goal is to have a 3D table which can be accessed as the last statement I placed. I agree with your comment, if any can figure it out, please tell me also. – Gerti Tuzi Aug 09 '12 at 14:19
  • @user1587730, I came up with something, but I don't trust it entirely myself. Anyway, like I said, best way is to use a `typedef`. – Shahbaz Aug 09 '12 at 14:19
  • 1
    Thank you Shahbaz, this seems to work too. The typdef definitively clears the clutter up. – Gerti Tuzi Aug 09 '12 at 14:35
1

you need

char (*threeDArr[2])[3] = {twoDArr, twoDArr};

with your solution the "innermost" specification was the * and not an array.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • Thank you for your reply. This does work. One additional question, if I want a temporary pointer, char*** cannot be assigned/equated/point-to to the array above. Any suggestion ? – Gerti Tuzi Aug 09 '12 at 14:25
  • I am not sure that I understand your additional question. The above is an array and not a pointer, you never can assign to it as a whole. And `char***` is a completely different beast, this is not compatible at all. – Jens Gustedt Aug 09 '12 at 14:35
  • Starting from the premise that a pointer points to a location in memory, what would be the pointer that can be used to point to the very first location to the array above, the - &threeDArr[0][0][0], let's call it: pointer_of_some_sort pT, so that pT[0][1][2] == threeDArr[0][1][2]. But I think using the typedef as Shahbaz suggested clears up my misunderstanding. I was regressing to elementary types. Using typedef char (*twoDArrP_type)[3], a twoDArrP_type* pT would accomplish what I was trying to. Again thank you for your help. – Gerti Tuzi Aug 09 '12 at 14:41
  • There is nothing easier than trying :) `char (**threeDArrP)[3] = threeDArr;` is now a pointer to pointer to char array of size 3. – Jens Gustedt Aug 09 '12 at 14:46