How to return an array of strings in C?

For example, I have in the main file

1) char ** array[NUMBER]; 2) array = build_array(); 

and in the imported file

 char ** build_array() { char ** array[NUMBER]; strings[0] = "A"; strings[1] = "B"; return (char *) strings; } 

However, on line 2 in the main file, I get the error message: "incompatible types when assigning to type 'char **[(unsighed int)NUMBER]' from type 'char **'

What am I doing wrong? Any suggestions or recommendations would be appreciated. Thank you in advance.

+4
source share
3 answers

There seems to be some confusion over the fact that a string is in C. In C, a zero-terminated sequence of char is considered a string. This is usually indicated by char* .

I just want to call the build_array() function and return an array of strings

You pretty much can't return an array, not a pointer to a local array. However, you could pass the build_array array as an argument, as well as its size and populate it instead.

 void build_array( char* strings[], size_t size ) { // make sure size >= 2 here, based on your actual logic strings[0] = "A"; strings[1] = "B"; } ...later called as:... char *array[NUMBER]; build_array(array, NUMBER); 

Alternatives are to return a pointer to a global or static allocated array, which will make your function irrevocable. It probably doesn’t care now, but it’s a bad practice, so I would recommend that you avoid this route.

+8
source

As littleadv pointed out, there are several problems with your code:

  • Inconsistency between char ** and char **[ ]

  • Returning a pointer to a local variable

  • Etc.

This example might help:

 #include <stdio.h> #include <string.h> #include <malloc.h> #define NUMBER 2 #define MAX_STRING 80 char ** build_array () { int i = 0; char **array = malloc (sizeof (char *) * NUMBER); if (!array) return NULL; for (i = 0; i < NUMBER; i++) { array[i] = malloc (MAX_STRING + 1); if (!array[i]) { free (array); return NULL; } } strncpy (array[0], "ABC", MAX_STRING); strncpy (array[1], "123", MAX_STRING); return array; } int main (int argc, char *argv[]) { char **my_array = build_array (); if (!my_array) { printf ("ERROR: Unable to allocate my_array!\n"); return 1; } else { printf ("my_array[0]=%s, my_array[1]=%s.\n", my_array[0], my_array[1]); } return 0; } 
+2
source

Your return type is char** , and you assign it to char**[] , which is incompatible.

In addition, you must publish the actual code with which you are having problems, the code that you published does not compile and does not make much sense.

To fix your code, the function must return char **[NUMBER] . Also note that you are returning the char* return value instead of the char** that you declared (or char **[NUMBER] that it should be, but actually is).

Oh, and returning a pointer to a local variable, as in your case, is the perfect recipe for crashes and undefined behavior.

What do you probably mean:

 char *array[NUMBER]; int ret = build_array(array, NUMBER); // do something with return value or ignore it 

and in the imported file

 int build_array(char **arr, int size) { // check that the size is large enough, and that the // arr pointer is not null, use the return value to // signal errors arr[0] = "A"; arr[1] = "B"; return 0; // asume 0 is OK, use enums or defines for that } 
+1
source

All Articles