Thread: char[][] instead of char*[] in calling a function?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    1

    char[][] instead of char*[] in calling a function?

    Hi All,

    I have a function that takes 'char *[]' as parameter:
    Code:
    void myFunc(char *mystrings[]){}
    In order to call it I need to create an array of 'char *' and call it. (with malloc and all)

    Is there anyways to call this function with char[][] instead of char*[]? Something like this:
    Code:
    char mystring[2][20];
    
    strcpy(mystring[0], "This is first line");
    strcpy(mystring[1], "This is second line"); 
    myFunc(mystring);
    I've tried myFunc(*mystring) and myFunc(**mystring) already.

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mishoo123
    Is there anyways to call this function with char[][] instead of char*[]?
    Not directly since an array of arrays is converted to a pointer to an array, not a pointer to a pointer. If necessary you could create an array of pointers, get those pointers to point to the first elements of those inner arrays, then pass the array of pointers which will be converted to a pointer to a pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-16-2008, 03:46 PM
  2. calling a char *function
    By JFonseka in forum C Programming
    Replies: 9
    Last Post: 04-13-2008, 06:25 AM
  3. Function input may char or char[] ..?
    By GSalah in forum C++ Programming
    Replies: 2
    Last Post: 11-15-2006, 04:07 AM
  4. Read File To Char Array with Null char init
    By MicroFiend in forum Windows Programming
    Replies: 1
    Last Post: 10-28-2003, 06:18 PM
  5. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM