Solved

Number of arguments in a va_list ?

Posted on 2003-12-02
8
1,096 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

695 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