0

I'm trying to return a char array from a function. According to everything I have read I understand I cannot return the array itself because arrays are second class citizens. But from what I understand I can create a pointer to it and make it static. I have tried both and am failing to compile. Any ideas why?

int main ()
{
  char *hold_array[] = get_array ();                                                              
  return 0;
}

char *get_array (void)
{
   static char *array[] = {"test1", "test2"};
   return (array);
}
1
  • Two problems: 1, hold_array and the return value of get_array should be char **, not char *. 2, char_array() has to be declared before main() or it will be assumed to return int. Commented Jun 26, 2013 at 2:36

4 Answers 4

2

compile error because of type mismatch. @JustinPeel points out that const is needed too.

int main(void)
{
    char **hold_array = get_array ();
    return 0;
}

char **get_array(void)
{
     static const char *array[] = {"test1", "test2"};
     return (array);
}
Sign up to request clarification or add additional context in comments.

3 Comments

This compiles with warnings for me because you then have non-const pointers to a string literal. The OP should really either change the chars to const chars or use a helper function that uses mallocs. My guess is that these strings really are supposed to be const.
@JustinPeel You are right, this is not a good practice, using malloc is a better solution generally.
it is perfectly fine to use do static const char *array[] = {"test1", test2"}; too though.
1

The *hold_array[] is an array of pointers. The function get_array returns a single pointer, albeit that the returned pointer is a pointer to an array of char * pointers. However, you cannot declare *hold_array[] as an array of pointers without any dimension. The compiler has no idea how much space to allocate for it. It doesn't know how many pointers you'll need.

You can declare it as **hold_array. This is a single pointer which points to an array of pointers. I know it sounds the same, but it isn't exactly. In the case of *hold_array[] you are allocating multiple pointers and therefore need a dimension. In the case of **hold_array you are allocating one pointer which points to an array of pointers. In your initializer static char *array[] the compiler sees you need two pointers and internally dimensions it for you as *array[2].

Therefore in main() you are asking the compiler to create an array of unknown dimension which consists of char * pointers. The function get_array returns the address of an array of pointers, but not the individual pointers within the array. Remember that array[0] is a pointer to the string "test1" and array[1] is a pointer to the string "test2". The strings themselves are character arrays with a 0 byte at the end.

Another way to think of it is:

char *test1 = "test1";
char *test2 = "test2";

char *array[2];
char **hold_array;

array[0] = test1;
array[1] = test2;

hold_array = array;

printf("\n%s = %s", hold_array[0], array[0]);  //print the same string

Therefore, when you return array the compiler assumes you mean the array itself since you haven't otherwise specified a member of the array. So only the address of array itself is returned. This is identical to **hold_array. Which means hold_array = array. **hold_array is a single pointer which points to an array of pointers. array[2] is an array of pointers which allocated space for 2 pointers.

Comments

1

Try changing

char *hold_array[] = get_array ();

to

char **hold_array = get_array ();

Or (better still IHMO) to use malloc.

Comments

1

For what you want to do it is best to allocate the array dynamically using malloc and then return a pointer to the allocated memory.

E.g.:

char* char_array_ptr = malloc(sizeof(char) * size_of_array);

Do note that sizeof(char) is redundant since the char type is guaranteed to be 1 by the standard, though you will need sizeof for other types.

You function should have a signature similar to char* return_array() and you should return the pointer to the allocated memory at the end of the function - return array_ptr;

All in all we would have something like:

char* allocate_array()
{
    char* ptr = malloc(20); /* size here will be 20 */

    strcpy(ptr, "Just a normal string."); /* copy string to that pointer */

    return ptr; /* return pointer for further use */
}

Don't forget to free the memory allocated with malloc once you're done using it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.