简体   繁体   中英

C program malloc with array of strings

At the beginning of a program I need to dynamically allocate memory for an unknown number of strings with unknown number of size to later manipulate with. To get the number of strings from a user I have:

int main(int argc, char *argv[]){

int number = atoi(argv[1]);

So far so good. "number" now holds holds the number that the user inputted on the command line for executing the code. Now here comes the part I don't quite understand. I now need to dynamically store the lengths of the strings as well as the contents of the strings. For example, I want the program to function like this:

Enter the length of string 1: 5
Please enter string 1: hello
Enter the length of string 2: ...

For this I recognize that I will have to create an array of strings. However, I can't quite understand the concept of pointers to pointers and what not. What I would like is perhaps a simplification of how this gets accomplished?

You know from the start you will have number strings to store so you will need an array of size number to store a pointer to each string.

You can use malloc to dynamically allocate enough memory for number char pointers:

char** strings = malloc(number * sizeof(char*));

Now you can loop number times and allocate each string dynamically:

for (int i = 0; i < number; i++) {
   // Get length of string
   printf("Enter the length of string %d: ", i);
   int length = 0;
   scanf("%d", &length);

   // Clear stdin for next input
   int c = getchar(); while (c != '\n' && c != EOF) c = getchar();

   // Allocate "length" characters and read in string
   printf("Please enter string %d: ", i);
   strings[i] = malloc(length * sizeof(char));
   fgets(strings[i], length, stdin);
}

Since you want to save both the length and the string, I'll suggest that you put them together in a struct. Like

struct string
{
    int length;
    char* str;
};

Now you can dynamically create an array of this struct and dynamically assign memory for the individual strings.

Something like:

#include <stdio.h>
#include <stdlib.h>

struct string
{
    int length;
    char* str;
};

int main(void) {
    int i;
    char tmp[128];
    int number = 3;
    struct string* strings = malloc(number * sizeof *strings);

    // read the input
    for (i=0; i<number; ++i)
    {
        printf("length?\n");
        if (fgets(tmp, sizeof tmp, stdin) == NULL)
        {
            printf("error 1");
            exit(1);
        }
        int length;
        if (sscanf(tmp, "%d", &length) != 1)
        {
            printf("error 2");
            exit(1);
        }
        strings[i].length = length;
        strings[i].str = calloc(length + 2, 1);
        printf("string?\n");
        if (fgets(strings[i].str, length + 2, stdin) == NULL)
        {
            printf("error 3");
            exit(1);
        }
        if (strings[i].str[length] != '\n')
        {
            printf("error 4");
            exit(1);
        }
        strings[i].str[length] = '\0';
    }

    // print the strings
    for (i=0; i<number; ++i)
    {
      printf("len=%d str=%s\n", strings[i].length, strings[i].str);
    }

    // Clean up, i.e. free the memory allocated
    for (i=0; i<number; ++i)
    {
        free(strings[i].str);
    }
    free(strings);

    return 0;
}

Note : You should also check that all malloc / calloc are succesful, ie doesn't return NULL. For clarity I skipped that.

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