Casting a char[x][x] to char *[]?
So I have some code like
Code:
static void myfunc(char * val[])
{
if(*(val[0]) == 'x')
//do stuff
}
char foo[30][30];
//populate foo
myfunc(foo);
This won't compile. I tried myfunc((char *[])foo); but that wouldn't compile either. I could only get it to compiile with myfunc((char **)foo);. But this doesn't run because the the value at val[0] gets treated like a pointer. So I tried val[0][0], but the same thing happened.
Is this because I'm casting to char ** when I pass it?
Re: Casting a char[x][x] to char *[]?
The problem is that val is a pointer to a pointer, but foo is an array of arrays. When an array of arrays is passed as an argument, it is converted to a pointer to an array, not a pointer to a pointer.
Can you change the parameter of myfunc to accommodate foo? If not, one possibility is to create an array of pointers such that each pointer points to a corresponding inner array of foo. Passing this array of pointers will then yield a pointer to a pointer that is exactly what myfunc requires.
Re: Casting a char[x][x] to char *[]?
You can always do something like this:
Code:
static void MyFunction(char *val) {
if (val[0] == 'x') {
//...
}
}
char foo[50][50];
//...
MyFunction(*foo);
Re: Casting a char[x][x] to char *[]?
Quote:
Originally Posted by
laserlight
Can you change the parameter of myfunc to accommodate foo? If not, one possibility is to create an array of pointers such that each pointer points to a corresponding inner array of foo. Passing this array of pointers will then yield a pointer to a pointer that is exactly what myfunc requires.
No, I can't change myfunc's params. Too much other code uses it. I was hoping to avoid malloc, but it looks like I'll have to do char *foo[30] and then malloc each index.
Re: Casting a char[x][x] to char *[]?
You don't have to use malloc if you are willing to create a parallel array of pointers.