简体   繁体   中英

how to dynamically allocate memory for a struct type array in C

so I have a program where I need to read a file and store some words from it in an array, but I want to do it so that in each array in which I store the respective words to have custom size depending on how many words I have

typedef struct {
    char* keyword;
    int keywordCount;
    int stdev;
} keywordData;

int keywordNumber;

keywordData* keyword;

void fetchKeywords(const char* filename)
{
    FILE* keywords = fopen(filename, "r");
    // first number in the file is the number of keywords in the file, so i dont need to count them
    fscanf(keywords,"%d", &keywordNumber);

    keyword = (keywordData *) malloc(keywordNumber * sizeof(keywordData));

    for(int i = 0; i < keywordNumber; i++)
    {
        fscanf(keywords,"%s", keyword[i].keyword);
        //printf("%s\n", keyword[i].keyword);
    }
}

Your code is invalid as you did not allocate memory for keyword and you scan the string into it. It invokes undefined behaviour UB.

Try not to use global variables. Use function return values and if needed pointer parameters.

#define MAXKEYWORDLENGTH 64

typedef struct {
    char keyword[MAXKEYWORDLENGTH];
    int keywordCount;
    int stdev;
} keywordData;


keywordData *fetchKeywords(const char* filename, int *keywordNumber)
{
    FILE* keywords = fopen(filename, "r");
    keywordData *kd;
    // first number in the file is the number of keywords in the file, so i dont need to count them
    if(keywords) 
       if(fscanf(keywords,"%d", keywordNumber) != 1) { /* error handling*/}

    kd = malloc(*keywordNumber * sizeof(*kd));

    if(kd)
        for(int i = 0; i < keywordNumber; i++)
        {
            fscanf(keywords,"%s", kd[i].keyword);
            //printf("%s\n", keyword[i].keyword);
        }
    if(keywords) fclose(keywords);
    return kd;
}

I don't have enough rep to comment, but the warning you're getting with the other answer's code is because keywordNumber is an int * whereas i is just an int , so you need to dereference keywordNumber first in the for loop (so for (int i = 0; i < *keywordNumber; i++) ); this code should fix it:

#define MAXKEYWORDLENGTH 64

typedef struct {
    char keyword[MAXKEYWORDLENGTH];
    int keywordCount;
    int stdev;
} keywordData;


keywordData *fetchKeywords(const char* filename, int *keywordNumber)
{
    FILE* keywords = fopen(filename, "r");
    keywordData *kd;
    // first number in the file is the number of keywords in the file, so i dont need to count them
    if(keywords) 
       if(fscanf(keywords,"%d", keywordNumber) != 1) { /* error handling*/}

    kd = malloc(*keywordNumber * sizeof(*kd));

    if(kd)
        for(int i = 0; i < *keywordNumber; i++)
        {
            fscanf(keywords,"%s", kd[i].keyword);
            //printf("%s\n", keyword[i].keyword);
        }
    if(keywords) fclose(keywords);
    return kd;
}

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