Ykt*_*ula 3 c linked-list c99 variadic-functions
我想用指针参数初始化一个链表,如下所示:
/*
* Initialize a linked list using variadic arguments
* Returns the number of structures initialized
*/
int init_structures(struct structure *first, ...)
{
struct structure *s;
unsigned int count = 0;
va_list va;
va_start(va, first);
for (s = first; s != NULL; s = va_arg(va, (struct structure *))) {
if ((s = malloc(sizeof(struct structure))) == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
count++;
}
va_end(va);
return count;
}
Run Code Online (Sandbox Code Playgroud)
问题是,铛错误type name requires a specifier or qualifier的va_arg(va, (struct structure *)),并说该类型说明符默认为int.报告还指出实例化的形式在(struct structure *)和struct structure *.这似乎是得到分配s的int (struct structure *).
当删除括号时,它编译得很好(struct structure *),但是应该初始化的结构是不可访问的.
为什么int假设括号围绕传递给va_arg的类型参数?我怎样才能解决这个问题?
va_arg在许多系统上都是一个宏,显然圆括号struct structure *会导致宏扩展,因此无法解析.所以不要这样做.
这与您的初始化结构"无法访问"的原因无关.您正在分配结构并将其分配给s,但它s是一个局部变量.您不能通过分配局部变量来影响调用者中的值.要完成你想要做的事情,调用者需要传递一个指向指针的指针,然后你可以初始化它
int init_structures(struct structure **first, ...)
{
struct structure **s;
unsigned int count = 0;
va_list va;
va_start(va, first);
for (s = first; s != NULL; s = va_arg(va, struct structure **)) {
if ((*s = malloc(sizeof(struct structure))) == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
count++;
}
va_end(va);
return count;
}
Run Code Online (Sandbox Code Playgroud)
调用者应该这样做:
struct structure *a, *b;
init_structures(&a, &b, NULL);
Run Code Online (Sandbox Code Playgroud)