473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

va_list help, please ...

--------------------------------------------------------------------------------

Hi,

I was wondering if we can create a va_list by adding objects in the
va_list instead of passing in the parameter preceding first optional
argument? In another words, I want to create a va_list in a similar
manner as creating an array.

The reason is I want to use FormatMessage() in my program where it
takes in a va_list as an argument, but the objects that I would like
to pass in with the va_list is not in the form of a parameter
argument.

I tried doing the following but obviously it won't work, but I have no
idea:

va_list temp;
va_arg(temp, long) = value1;
va_arg(temp, long) = value2;

ANy help would be greatly appreciated,

Thanks,
Peter
Jul 23 '05 #1
6 3669
Peter wrote:

I tried doing the following but obviously it won't work, but I have no
idea:

va_list temp;
va_arg(temp, long) = value1;
va_arg(temp, long) = value2;

ANy help would be greatly appreciated,


Write a variadic function that calls the function you're interested in.

void wrapper(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
FormatMessage(fmt, ap); // whatever you need here
va_end(ap);
}

Now call wrapper from the spot where you know what your arguments are:

wrapper(fmt, value1, value2);

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #2
Peter wrote:
I was wondering if we can create a va_list by adding objects in the
va_list instead of passing in the parameter preceding first optional
argument? In another words, I want to create a va_list in a similar
manner as creating an array.

The reason is I want to use FormatMessage() in my program where it
takes in a va_list as an argument, but the objects that I would like
to pass in with the va_list is not in the form of a parameter
argument.

I tried doing the following but obviously it won't work, but I have no
idea:

va_list temp;
va_arg(temp, long) = value1;
va_arg(temp, long) = value2;

ANy help would be greatly appreciated,


I'd create a proxy function with ellipsis in the argument list and then
in it I'd implement the forming of va_list using va_args which I'd after
creating it passed to whatever function I need:

int MyFormatMessage(other_arguments, int lastone, ...)
{
va_list lst;
va_start(lst, lastone);
FormatMessage(blahblahblah, lst);
va_end(lst);
}

That's the way to create your own "printf"-like functions that use vprintf
to actually print.

V
Jul 23 '05 #3
Thank you very much for your suggestions. I think that's a good idea.
The only thing is that I was wondering if I can create something that
is more generic. Because I don't want to just create a single wrapper
function for a single call in the program.

The problem is, the parameters' type can be different types (ex, long,
int ....etc ). Does anybody know how I can write the wrapper Pete
suggested into something that takes in like generic parameters? Is that
even possible?

Thanks a lot,
Peter

Jul 23 '05 #4
Actually, for the two wrappers you guys suggested, would it work if the
parameter is int or long? Doesn't FormatMessage takes only
null-terminated strings? So even if we have a va_list but the arguments
in the va_list are long/int, so it still won't work right? Does that
mean I still need to convert my long variables to char* right? How can
I do that in a "better" way than just using casting>?

Jul 23 '05 #5
ks****@gmail.com wrote:
Actually, for the two wrappers you guys suggested, would it work if the
parameter is int or long?
Shouldn't matter.
Doesn't FormatMessage takes only
null-terminated strings?
'FormatMessage' is not a standard function. If you're talking about MS
Windows API 'FormatMessage', then just RTFM. It's OT here anyway.
So even if we have a va_list but the arguments
in the va_list are long/int, so it still won't work right?
Should, AFAIUI.
Does that
mean I still need to convert my long variables to char* right?
What????
How can
I do that in a "better" way than just using casting>?


What are you talking about?
Jul 23 '05 #6
ks****@gmail.com wrote:
Thank you very much for your suggestions. I think that's a good idea.
The only thing is that I was wondering if I can create something that
is more generic. Because I don't want to just create a single wrapper
function for a single call in the program.

The problem is, the parameters' type can be different types (ex, long,
int ....etc ). Does anybody know how I can write the wrapper Pete
suggested into something that takes in like generic parameters? Is that
even possible?


That wrapper function does just what you need. Think about printf --
it's the same sort of thing.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alex | last post by:
Can anyone point me to any good online references about the va_arg() va_list() functions/macros I googled and found a couple of things, but what I found, failed to explain how it really works...
3
by: shiva | last post by:
hello, just i want to write a program which can take unknown number of integers and sum these numbers using va_list,va_arg... but, i am not able to recognize the last element in series.can u...
7
by: Jon | last post by:
Is it possible to modify an argument of a va_list and then pass the modified va_list to other functions? void c_fun1(int, va_list args) { int *firstIntPointer = &(va_arg(args, int)); ...
1
by: skillzero | last post by:
Is there a portable way to pass a va_list as a parameter to another function taking a variable argument list? I have a function that takes a printf-like format string and I'd like to use...
6
by: ramu | last post by:
Hi, Can we intialize va_list to NULL? ie. va_list = NULL Regards
8
by: Fred | last post by:
I've got following program encapsuled fscanf, however, it doesn't work. I'm sure that the content format in "a.txt" is OK, the content would be correctly read if using fscanf directly, what's...
5
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
On Jun 3, 3:23 am, Jesse Ziser <d...@spam.diewrote: The relevant paragraph from the Standard is: ---- Begin Quote ---- The type declared is va_list which is an object type suitable for...
6
by: Laurent Deniau | last post by:
When I compile the code below with gcc -std=c99 -W -Wall -pedantic -O3 -Winline, it reports the following: variadic.c: In function ‘fv’: variadic.c:12: warning: function ‘fv’ can never be...
10
by: raphfrk | last post by:
Is there any way to pass a variable list to a function, so as to act as a passthrough? for example: void new_printf ( char *buf , ... ) { va_list vlist; <modify buf slightly>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.