va_arg with pointers

Posted by Yktula on Stack Overflow See other posts from Stack Overflow or by Yktula
Published on 2010-05-16T21:53:30Z Indexed on 2010/05/16 22:00 UTC
Read the original article Hit count: 263

Filed under:
|
|
|

I want to initialize a linked list with pointer arguments like so:

/* 
 * 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;
}

The problem is that clang errors type name requires a specifier or qualifier at va_arg(va, (struct structure *)), and says that the type specifier defaults to int. It also notes instantiated form at (struct structure *) and struct structure *. This, what seems to be getting assigned to s is int (struct structure *).

It compiles fine when parentheses are removed from (struct structure *), but the structures that are supposed to be initialized are inaccessible.

Why is int assumed when parentheses are around the type argument passed to va_arg? How can I fix this?

© Stack Overflow or respective owner

Related posts about c99

Related posts about c