Solved

Number of arguments in a va_list ?

Posted on 2003-12-02
8
1,082 Views
Last Modified: 2008-02-01
How can I get the count of arguments in va_list ?

I want to program my own printf-function. So I want to know count of args or end of arg-list in myprintf(const char _FAR *__format, ...) - function.

Thanx
 bandolero
0
Comment
Question by:bandolero
8 Comments
 
LVL 17

Expert Comment

by:rstaveley
ID: 9858658
I think you can't. printf() uses the format to figure out how many arguments ought to be there and hopes that they are.
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9858784
You will have to traverse thru the arguments to know the count

Help for all the required macros

Data Type: va_list

The type va_list is used for argument pointer variables.

Macro: void va_start (va_list ap, last-required)

This macro initializes the argument pointer variable ap to point to the first of the optional arguments of the current function; last-required must be the last required argument to the function.

See Old Varargs, for an alternate definition of va_start found in the header file varargs.h.

Macro: type va_arg (va_list ap, type)

The va_arg macro returns the value of the next optional argument, and modifies the value of ap to point to the subsequent argument.  Thus, successive uses of va_arg return successive optional arguments.

The type of the value returned by va_arg is type as specified in the call.  type must be a self-promoting type (not char or short int or float) that matches the type of the actual argument.

Macro: void va_end (va_list ap)

This ends the use of ap.  After a va_end call, further va_arg calls with the same ap may not work.  You should invoke va_end before returning from the function in which va_start was invoked with the same ap argument.

In the GNU C library, va_end does nothing, and you need not ever use it except for reasons of portability.


Sometimes it is necessary to parse the list of parameters more than once or one wants to remember a certain position in the parameter list.  To do this, one will have to make a copy of the current value of the argument.  But va_list is an opaque type and one cannot necessarily assign the value of one variable of type va_list to another variable of the same type.

Macro: void __va_copy (va_list dest, va_list src)

The __va_copy macro allows copying of objects of type va_list even if this is not an integral type.  The argument pointer in dest is initialized to point to the same argument as the pointer in src.

This macro is a GNU extension but it will hopefully also be available in the next update of the ISO C standard.

If you want to use __va_copy you should always be prepared for the possibility that this macro will not be available.  On architectures where a simple assignment is invalid, hopefully __va_copy will be available, so one should always write something like this:


{
  va_list ap, save;
  ...
#ifdef __va_copy
  __va_copy (save, ap);
#else
  save = ap;
#endif
  ...
}

HTH

Amit
0
 
LVL 4

Expert Comment

by:YuriPutivsky
ID: 9859882
I don't think there is a way to know the number of arguments of va_list.
Generally speaking it's a pointer to the current stack position and how many arguments were pushed to the stack depends on the caller code
Use vprintf instead
int vprintf( const char *format, va_list argptr );

0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9860784
You cannot know the number of arguments passed unless the caller cooperates.  

Typically (as in printf), the function can look for something in the *first* argument (such as the number of %s and %d codings in the format string) or you can require that the first parameter be a count of the number of parameters that follow.    Or you can require that the *last*  argument have a special value (such as 0 or -1).

That is why varargs functions are considred to be "unsafe"  -- you can only *hope* that the caller followed the conventions... you cannot enforce it.   And a small programming mistake can be hard to locate -- catastophic errors typically appear only in certain conditions, based sometimes upon validity of user input.

-- Dan
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 2

Accepted Solution

by:
colmcc earned 125 total points
ID: 9863967
In principle, it's quite easy to implement your own version of printf and related functions.  (There's even a example in K&R I think.)

If you just want to get hold of the normal output that printf would have produced, in order to display it in a window for example, then it's almost trivial -

int myprintf(char * fmt, ...)
{
 int nChars;
 char szBuf[BUFSIZE];
 va_list args;

 va_start(args,fmt);

 nChars = vsprintf(szBuf,args);

 // Do something with szBuf here.

 va_end(args);

 return (nChars);
}
 
It's a lot more complicated if you want to implement your own format specifiers like %s etc.  Unless there is a way of inheriting the standard printf behaviour and just adding your own new ones, then you have to implement it all from scratch.  I believe the example in K&R does this for just a few of the possible specifiers, but does at least demonstate a technique.

Regards,
Colin
0
 
LVL 5

Expert Comment

by:dennis_george
ID: 9864263
Hi,
  There is no Direct way to get the number of arguements of a va_list...... either you have to pass the number of arguements or you have to device a method to get the number of arguements from the first aguement.....

I think this topic has been already been covered......
check the following questions.......
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20787262.html


Dennis
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9864979
Oh!  Oh!  I want to change my answer!

42
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9865610
Take a slug from that Pan Galactic Gargle Blaster :-)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now