CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2008
    Posts
    18

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 2011
    Posts
    1

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

  4. #4
    Join Date
    Nov 2008
    Posts
    18

    Re: Casting a char[x][x] to char *[]?

    Quote Originally Posted by laserlight View Post
    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured