简体   繁体   中英

C: Passing an array of pointers to structs as an argument

This question may well have already been answered so I will explain very simply:

I have defined a "list" structure. In a separate function I have initialised an array called "table":

int table_size = 500;
struct list* table[table_size];

This will store pointers to these "list" structures. Later in the function I pass this table to another function as the third argument:

generate(word, table_size, table);

This "generate" function is defined in the following way:

void generate(char *str, int table_size, struct list* table)

And when I try to compile, I get the following error:

passing argument 3 of 'generate' from incompatible pointer type [-Werror]

note: expected 'struct list *' but argument is of type 'struct list **'

Thanks to anyone who can explain what's going wrong.

Your generate function has the wrong type. Try

void generate(char *str, int table_size, struct list** table)

or

void generate(char *str, int table_size, struct list* table[])

答案在错误消息中:将参数更改为struct list **类型。

Your declaration of table is an array of struct list pointers. It is compiled as a pointer to a pointer of struct list. Declare table like this:

struct list table[table_size]

I am assuming you want an array of struct list. Follow the above answers if otherwise.

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