简体   繁体   English

如何为 C 中的结构类型数组动态分配 memory

[英]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.您的代码无效,因为您没有为keyword分配 memory 并且您将字符串扫描到其中。 It invokes undefined behaviour UB.它调用未定义的行为 UB。

Try not to use global variables.尽量不要使用全局变量。 Use function return values and if needed pointer parameters.使用 function 返回值和指针参数(如果需要)。

#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++) );我没有足够的代表发表评论,但是您收到其他答案代码的警告是因为keywordNumber是一个int *i只是一个int ,所以您需要在 for 循环中首先取消引用keywordNumber (所以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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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