0

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.

3
Contributors
9
Replies
14
Views
9 Years
Discussion Span
Last Post by ArkM
2

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...

Votes + Comments
Thankyou :)
Good catch :)
0

>>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.

0

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...

0

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);
}
0

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

0

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

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

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.