简体   繁体   中英

Function that returns array of strings in C

I'm new to C and I'm currently working on a project that I need to read a list from a Json file (using json-c library ). So, I created a function that returns an array of string (the list). I searched and I found out that to return array of string in a function you need to type something like:

char** readJson(...) {
    ...
}

and in the main:

int main() {
    ...
    char** list = readJson();
    ...
}

My problem is that when I create my array, in the read function, I don't declare it, I loop through the list items (from the json object) and I add the them in the list. For example, I don't:

char** list = {"...", "..."}

I do:

char** list;
for (int i = 0; i < LIST_SIZE; i++) {
    strcpy(list[i], json_object_get_string(json_obj));
}

And when I try to print the list item after strcpy (in for loop) it closes the program. So, I tried to create the array like that:

char list[LIST_SIZE][MAX_CHAR];

And then return this:

char** final = list;
return final;

And it worked. But , when I am returning the list it gives me a warning in the compiler:

main.c:66:20: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]

And when I try to print the list in main it crashes.

Here is the source code. Check it if you want.

Please, help me.

You cannot return an array from a function in C.

This is not a big problem, because you can always return a pointer to the first element of your array. Anyone who has such a pointer can access the whole array.

So here is a simple two step plan.

  1. Have an array.
  2. Have a pointer that points to its first element.

The only problem with that is as follows. If you return a pointer to something from a function, that something better be alive when the function returns. You do not want pointers to dead things floating around. However, normal variables declared in a function, including arrays, die when the function returns. So you need something else.

One way to overcome this is to make the array static or global variable, instead of normal (automatic) variable in a function. This has limited uses. Your array must be of a fixed size, and of course all calls to your function will return the same thing each time.

Another way to overcome this is to allocate the array dynamically using the malloc family of functions.

char* list = malloc(sizeof(char*) * number_of_elements_in_list);

list now points to the first element of a dynamically allocated array of char* .

Note that its elements are also pointers, and you need to initialize them correctly. How? Just read this answer again and apply it to each individual string. For example

for (int i = 0; i < number_of_elements_in_list; ++i)
    list[i] = read_next_json_string(json_object);

So you make an array available to your caller by returning a pointer to the first element of a dynamically allocated array. In your case, each element of your array is also a pointer pointing to the first element of a dynamically allocated array.

After your caller is done with the array, they should free the memory occupied by it. And in your case, also free memory occupied by each individual string — before freeing the main array!

As mentioned in the comments, you cannot simply interchange arrays and pointers. Based on your use-case, I would suggest using malloc() to dynamically allocate your list variable. Something along the lines of:

char **list = malloc(LIST_SIZE * sizeof(char *));
for(int i = 0; i < LIST_SIZE; i++)
    list[i] = malloc(MAX_CHAR * sizeof(char));

Doing so will still allow you to reference specific words/sentences by using list[i] and you can return list as a char ** :

char **result = list;
return result;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM