-
April 22nd, 2011, 01:14 PM
#1
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?
-
April 22nd, 2011, 01:20 PM
#2
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.
-
April 22nd, 2011, 02:07 PM
#3
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);
-
April 22nd, 2011, 02:22 PM
#4
Re: Casting a char[x][x] to char *[]?
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.
-
April 22nd, 2011, 10:05 PM
#5
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|