Hi, sorry if this has been asked before I did a search but couldn't find it.

How do I define a pointer to a 2D array? (I am trying to pass it into a function).

Thanks.

A 2d character array? void foo(char* ay[][255] ) or like this: void foo(char **ay)

Sorry, but char* ay[][255] declares 2D array of pointers to char.
Probably, coveredinflies wants to pass an array as a function argument so right example is most likely

void foo(char ay[][255] );

But it's not the same as

void foo(char **ay);

A pointer to an array is a funny thing in C and C++. Array name is implicitly converted in a pointer in all contexts except argument of sizeof operator. So no need in pointers to arrays.
But we can't pass 2D array declared as a[...][...] in the second foo because of its argument type is a pointer to a pointer to char, but not an implicit pointer to 2D array with 255 columns...

commented: Good catch :) +34
commented: Thankyou :) +1

>>Sorry, but char* ay[][255] declares 2D array of pointers to char.
You are right -- I should not have included the star.

We don't know how the OP declared the 2d array, so it could be either method.

It's impossible to declare 2D array which can be passed as type** if a declarator does not know "how to define a pointer to a 2D array". He/she must declare an array of pointers then fill the array with proper pointers to 1D arrays and so on... It's impossible...

Impossible? I would call the below a 2d array.

int main()
{
   const int rows = 25;
    const int columns = 255;
    char ** array = new char*[rows];
    for(int i = 0; i < rows; i++)
       array[i] = new char[columns];
   foo(array);
}

It seems you don't understand my previous post.
I never said that it's impossible for you (and me;))...

In the true sense of the word(s) "2D array" a construct with array of pointers is not C/C++ 2D array.

void foo(char ay[][255] );

Thanks this is what I needed. Pretty obvious in hindsight. Sorry for any ambiguity in original post.

It was Ancient Dragon's post (I have corrected a misprint only;)).

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.