指向另一个结构的结构

问题描述 投票:0回答:1

我正在尝试从一种结构指向另一种结构,以便为传入的每个文件制作words_list的链接列表,但这不起作用。例如,起初只有一个文件。以后可能会根据用户添加更多内容。传递给函数时出现错误

这里是结构:

typedef struct _word {
    char *s;                /* the word */
    int count;              /* number of times word occurs */
    int *line_numbers;      // Array of line numbers
    int num_line_numbers;   // Size of the array of line numbers
} word;
// Creating a struct to hold the data. I find it's easier
typedef struct _word_list {
    word *words;      // The array of word structs
    int num_words;    // The size of the array
} word_list;

typedef struct llist {
  word_list *fileno;
  struct llist *next;
} List;

函数头:

void insert_word (word_list *words, char *s, int line_number){..}

以及部分主要代码:

List *l;

l = malloc(sizeof(List));
l->fileno = malloc(sizeof(fileno));

l->fileno->words  = malloc(sizeof(word_list));
    if (NULL == l->fileno->words) exit(0);
    memset(l->fileno->words, 0, sizeof(word_list));
...
            insert_word(l->fileno->words, word, line_number); (it is inside a for loop, word and line_number work fine)

...
c pointers malloc structure
1个回答
0
投票

不是你的意思

l->fileno->words  = malloc(sizeof(word));

如果没有部分编译代码,这真的很难理解。

© www.soinside.com 2019 - 2024. All rights reserved.