Thread: how to create a function return with char* array

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    16

    how to create a function return with char* array

    i am a beginner to use c programming.
    how i am facing a problem about how to create a function with char * as a input parameter, and char *[] array as output parameter.

    any code reference.

    my steps:

    1.input parameter char * = "abc/def/ghi";
    2.in the function i want to slipt the char * into char * [] array by "/" as 3.delimiter. and finally out the char *[] as output.

    now i have the code in main fuction but i want to create indivdutal function to handle slipt string.
    Code:
    int main ()
    {
      char buf[] ="abc/qwe/ccd";
      int i;
      char *p;
      char *array[3];
      i = 0;
      p = strtok (buf,"/");  
      while (p != NULL)
      {
        array[i++] = p;
        p = strtok (NULL, "/");
      }
      for (i=0;i<3; ++i) 
        printf("%s\n", array[i]);
      return 0;
    }
    how to create a individual function to handle slipt string and output char * array[]?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like you already did the hard part of implementing the splitting logic, so you just need to move the code over to a function that might be declared as:
    Code:
    void split(char buf[], char *array[]);
    That said, you probably should have another parameter to specify the maximum number of elements of the array so as to avoid buffer overflow. The function could say, return the number of elements actually used instead of having void return type.
    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

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    16
    Quote Originally Posted by laserlight View Post
    It looks like you already did the hard part of implementing the splitting logic, so you just need to move the code over to a function that might be declared as:
    Code:
    void split(char buf[], char *array[]);
    That said, you probably should have another parameter to specify the maximum number of elements of the array so as to avoid buffer overflow. The function could say, return the number of elements actually used instead of having void return type.
    i want to create char*[] array as return object for split function, how can i do that?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tester1234
    i want to create char*[] array as return object for split function, how can i do that?
    You cannot actually return an array from a function, but you can return a pointer, so:
    Code:
    char **split(char buf[]);
    The problem is that you cannot (or rather should not) return a pointer to an element of a non-static local array, so you have two common choices:
    • Return a pointer to the first element of a static local array. This works very well for your specific example, but in practice this means the function has to decide to an upper bound for the array size that may prove to be too small.
    • Return a pointer to dynamically allocated memory. This generally works very well, except that it means the caller is responsible for freeing the memory.

    Another possibility is to go with my original suggestion and just return a pointer anyway, i.e., return array:
    Code:
    char **split(char buf[], char *array[]);
    but it may be a little pointless
    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

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    16
    Quote Originally Posted by laserlight View Post
    You cannot actually return an array from a function, but you can return a pointer, so:
    Code:
    char **split(char buf[]);
    The problem is that you cannot (or rather should not) return a pointer to an element of a non-static local array, so you have two common choices:
    • Return a pointer to the first element of a static local array. This works very well for your specific example, but in practice this means the function has to decide to an upper bound for the array size that may prove to be too small.
    • Return a pointer to dynamically allocated memory. This generally works very well, except that it means the caller is responsible for freeing the memory.

    Another possibility is to go with my original suggestion and just return a pointer anyway, i.e., return array:
    Code:
    char **split(char buf[], char *array[]);
    but it may be a little pointless
    i want to ask what is the different between char *[] and char**[]? if i return the pointer . how to use for loop to get all the elment of the returned array?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tester1234
    i want to ask what is the different between char *[] and char**[]?
    As used in the parameter list of a function, the former means "pointer to pointer to char" and the latter means "pointer to pointer to pointer to char". My guess is that you actually wanted to ask what char** meant in the return type, in which case it just means "pointer to pointer to char", but you should already know this.

    Quote Originally Posted by tester1234
    if i return the pointer . how to use for loop to get all the elment of the returned array?
    You have already done such a loop to print in your post #1.

    By the way, are you really trying to learn C and C++ at the same time? It is not impossible, especially if you already have a programming background, but for people new to programming it is a recipe for confusion and frustration.
    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

  7. #7
    Registered User
    Join Date
    Dec 2015
    Posts
    16
    Quote Originally Posted by laserlight View Post
    As used in the parameter list of a function, the former means "pointer to pointer to char" and the latter means "pointer to pointer to pointer to char". My guess is that you actually wanted to ask what char** meant in the return type, in which case it just means "pointer to pointer to char", but you should already know this.


    You have already done such a loop to print in your post #1.

    By the way, are you really trying to learn C and C++ at the same time? It is not impossible, especially if you already have a programming background, but for people new to programming it is a recipe for confusion and frustration.
    what is former = char* < only 1 *
    and latter char** ?
    is it correct?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tester1234
    what is former = char* < only 1 *
    and latter char** ?
    is it correct?
    This:
    Code:
    void split(char buf[], char *array[]);
    is equivalent to:
    Code:
    void split(char buf[], char **array);
    The reason why I used array notation in the former is to hint that an array is expected as an argument. Either way, the parameter named array is a pointer to a pointer to char.

    For this:
    Code:
    char **split(char buf[]);
    I went straight to char** notation because an array cannot be returned anyway (and besides, it would be invalid syntax).
    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. How to Return Char Array in function?
    By homoon in forum C Programming
    Replies: 3
    Last Post: 04-20-2012, 03:12 AM
  2. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  3. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  4. getting a function to return a char array
    By variable in forum C Programming
    Replies: 20
    Last Post: 02-10-2005, 08:13 PM
  5. I want to create a char array
    By dsingh in forum C Programming
    Replies: 3
    Last Post: 09-13-2004, 05:17 AM