EmanRuoy
EmanRuoy

Reputation: 87

Cast type to va_arg return

Can i cast type to va_arg return as on example? As i know va_arg can't read-return char/short int types

if (flags->size == 0)
    u->s = (char)va_arg(pt, int);
if (flags->size == 1)
    u->s = (short int)va_arg(pt, int);

Upvotes: 1

Views: 250

Answers (2)

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

From the documentation:

va_arg returns the current argument

So in your case it returns the value of what pt is pointing to as an int. So yes, you can cast that value.

Upvotes: 0

John Bollinger
John Bollinger

Reputation: 181734

The standard specifies that

The va_arg macro expands to an expression that has the specified type and the value of the next argument in the call.

(C2011 7.16.1.1/2)

The fact that such an expression resulted from expansion of a macro has no bearing on how it can be used. In particular, when the type specified to va_arg is int, the resulting int-type expression can certainly be cast to other integer types, provided that va_arg()'s behavior is defined (roughly meaning that int is compatible with the promoted type of the actual argument).

So yes, you can cast the result of va_arg().

Upvotes: 1

Related Questions