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.

11
  • When you say, doesn't compile, what's the actual error? Commented Aug 9, 2012 at 13:56
  • Your array has 3 members but you initialize it with only 2 members
    – giorashc
    Commented Aug 9, 2012 at 13:59
  • char (**threeDArr)[3] = {twoDArr, twoDArr, 0}; does work? Commented Aug 9, 2012 at 13:59
  • Define "work"! It possibly creates an array of three pointers, but they don't point to anything useful.
    – Bo Persson
    Commented Aug 9, 2012 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
    Commented Aug 9, 2012 at 14:08

2 Answers 2

1

you need

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

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

4
  • 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
    Commented Aug 9, 2012 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. Commented Aug 9, 2012 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
    Commented Aug 9, 2012 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. Commented Aug 9, 2012 at 14:46
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.

3
  • 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
    Commented Aug 9, 2012 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
    Commented Aug 9, 2012 at 14:19
  • 1
    Thank you Shahbaz, this seems to work too. The typdef definitively clears the clutter up.
    – Gerti Tuzi
    Commented Aug 9, 2012 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.