Question

I'm recoding printf in C. So i'm calling functions for each types of variables, so i prototyped all of them in a .h. But i got this error:

main.h:22:13: error: unknown type name ‘va_list’
     int dispdi(va_list ap);

Solutions?

Was it helpful?

Solution

If you are creating all of your own definitions, including va_list, then I assume the definition(s) you created in your .h looked at least similar to:

#ifndef _VA_LIST_DEFINED
#ifdef _M_CEE_PURE
typedef System::ArgIterator va_list;
#else
typedef char *  va_list;
#endif
#define _VA_LIST_DEFINED
#endif  

Just include this .h where ever you are using your version of printf

However, if you are using the standard C defines for va_list, va_start, va_arg et. al., then simply include stdarg.h.

In either case, here is an example of a simple variadic function (not printf, just a simple example) using the va_ macros:

#include <stdarg.h>

void variadic_function(int Param, ...) 
{
    int dslot;
    va_list params;
    va_start(params, Param);
    dslot = va_arg(params, int);
    va_end(args);
}  

For further reading, this Wiki article and its links round out the topic well.

OTHER TIPS

You should include the stdarg.h header file before using va_list and friends.

You header file may be something like:

#include <stdarg.h>
...
int my_printf (const char *format, ...);
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top