带指针的va_arg

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 qualifierva_arg(va, (struct structure *)),并说该类型说明符默认为int.报告还指出实例化的形式在(struct structure *)struct structure *.这似乎是得到分配sint (struct structure *).

当删除括号时,它编译得很好(struct structure *),但是应该初始化的结构是不可访问的.

为什么int假设括号围绕传递给va_arg的类型参数?我怎样才能解决这个问题?

JSB*_*ոգչ 5

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)


Mat*_*hen 5

C99 的§7.15.1.1(va_arg宏)要求:

参数类型应是指定的类型名称,这样指向具有指定类型的对象的指针的类型可以简单地通过将 * 后缀到类型来获得。

这就是为什么这里不允许使用括号的原因。

其他答案已经解释了为什么您需要传入struct structure **并将 malloc 结果分配给*s.