Va_lists and types

R

raphfrk

Does the va_list system automatically convert types? If a long is
passed, but va_arg is told that the input is actually an int, will the
conversion happen automatically?

For example, will this program always output the following, even
though long longs are passed rather than ints? If all va_arg did was
pop from the stack, then there could be problems.

Output:
1
2
3

Code:

#include <stdio.h>
#include <stdarg.h>

void va_func( int numargs , ... );

int main( int argc, int **argv )
{

va_func( 3 , (long long)1 , (long long)2 , (long long)3 );

}

void va_func( int numargs , ... )
{

va_list vp;

va_start( vp , numargs );

int x;

while( numargs > 0 )
{
x = va_arg( vp , int );
printf( "%d\n" , x );
numargs --;
}

va_end( vp );

}
 
E

Eric Sosman

Does the va_list system automatically convert types? If a long is
passed, but va_arg is told that the input is actually an int, will the
conversion happen automatically?

No. The type you use in va_arg must be compatible with
the actual type of the corresponding argument.

There is a *little* bit of "conversion" going on, though,
in the call to the function. Since the caller doesn't know
the types of the `...' parameters, it applies the "default
argument promotions." Any float argument is converted to
double, and any narrower-than-int argument is converted to
int (or in some cases to unsigned int). When you use va_arg
to retrieve one of these values, you must specify the promoted
type (e.g., double, int) and not the original (float, short).
For example, will this program always output the following, even
though long longs are passed rather than ints?[...]

No: The behavior is undefined because the type used with
va_arg does not match the type of the argument.
 
R

raphfrk

No. The type you use in va_arg must be compatible with
the actual type of the corresponding argument.

There is a *little* bit of "conversion" going on, though,
in the call to the function. Since the caller doesn't know
the types of the `...' parameters, it applies the "default
argument promotions." Any float argument is converted to
double, and any narrower-than-int argument is converted to
int (or in some cases to unsigned int). When you use va_arg
to retrieve one of these values, you must specify the promoted
type (e.g., double, int) and not the original (float, short).

Ahh, interesting. I was wondering how it handled that stuff.
For example, will this program always output the following, even
though long longs are passed rather than ints?[...]

No: The behavior is undefined because the type used with
va_arg does not match the type of the argument.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top