Va_arg with pointers

I want to initialize a linked list with pointer arguments as follows:

/* 
 * 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 are type name requires a specifier or qualifierin va_arg(va, (struct structure *))and says that the type specifier defaults to int. He also notes a copy of the form at (struct structure *)and struct structure *. It seems to be assigned s: int (struct structure *).

It compiles fine when the brackets are removed from (struct structure *), but the structures that need to be initialized are not available.

Why is intit supposed when parentheses are around the type argument passed to va_arg? How can i fix this?

+3
source share
3

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

:

struct structure *a, *b;
init_structures(&a, &b, NULL);
+5

ยง7.15.1.1 ( va_arg) C99 :

, , , *.

.

, struct structure ** malloc *s.

+4

va_arg. . va_arg - . CPP, - . , , . , .

+1

Source: https://habr.com/ru/post/1745694/


All Articles