1

I try to implement source code as below:

bool getParam(char* cmd, char** prm_arr, int num)
{
}

void main()
{
     char strC[] = "btOK,btCancel";
     char foo[10][10];
     bool res = getParam(strC,foo,2);
}

It shows error:

error: cannot convert ‘char (*)[10]’ to ‘char**’ for argument ‘2’ to ‘bool getParam(char*, char**, int)’
     bool res = getParam(strC,foo,2);

I think char** and char (*)[10] is similar in this case, isn't it?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
ThomasD
  • 23
  • 5
  • An array is not a pointer, and a pointer is not an array. In particular, a pointer to an array is not a pointer to a pointer. – molbdnilo Sep 23 '19 at 09:24
  • If you could do this, the function could change `*prm_arr` to point somewhere else. But the thing being changed is 10 elements of a larger array; being able to "make it point somewhere else" makes no sense. – chris Sep 23 '19 at 09:26
  • `char foo[10][10]` will allocate 100 chars arranged as 10 blocks of 10 chars each. It will not create an array of pointers to each block. Therefore you cannot get a pointer to a pointer array (`char **`). The solution depends on what you want to achieve. Do you need to pass two-dimensional arrays of variable size to `getParam()`? – nielsen Sep 23 '19 at 09:36

1 Answers1

1

The array declared like

char foo[10][10];

is converted to the type char( * )[10] when is passed to the function. And there is no implicit conversion from the type char ( * )[10] to the type char *.

So the function declaration should be

bool getParam(char* cmd, char ( *prm_arr )[10], int num);

That is in expressions with rare exceptions arrays are converted to pointers to array elements type.

If you have an array of the type T as for example

T a[N];

when the array is converted to the type T *.

In the declaration of the array foo the type of its elements is char[10]. So the array is converted to pointer to the element type char ( * )[10]

Pay attention to that the function main shall be declared like

int main()

instead of

void main()
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • In case, I want to keep a function declaration of getParam. How can i pass foo into getParam(). Actually, foo is allocated dynamically base on type "cmd" command. – ThomasD Sep 23 '19 at 10:15
  • @ThaiDao For example you can allocate a dynamic array of pointers to char. char **p = new char *[10]; And then in a loop allocate arrays of the type char[10]. – Vlad from Moscow Sep 23 '19 at 10:26