473,799 Members | 3,224 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3695
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(f mt, 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_argument s, int lastone, ...)
{
va_list lst;
va_start(lst, lastone);
FormatMessage(b lahblahblah, 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.co m 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.co m 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
2309
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 and give a easy to follow example program Any pointers appreciated.
3
9459
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 help in this regard. i want a generalized program , i dont want to terminate my series with 0 or any other pre-determined number.
7
4852
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)); *firstIntPointer = 2; // a new value }
1
9476
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 something like %V to pass in another format string and a va_list to allow nesting. It happens to work on my compiler, but I wasn't sure if it's portable to use va_list's as parameters to a variable argument function because va_list isn't always just a...
6
11683
by: ramu | last post by:
Hi, Can we intialize va_list to NULL? ie. va_list = NULL Regards
8
2777
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 wrong with my program? Thanks for help! int vsscanf( FILE* fp, const char *fmt, ... ) { va_list arglist; va_start( arglist, fmt );
5
4669
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 holding information needed by the macros
6
4068
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 inlined because it uses variable argument lists variadic.c: In function ‘vf’: variadic.c:12: warning: inlining failed in call to ‘fv’: function not inlinable variadic.c:27: warning: called from here
10
4649
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
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10470
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10214
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10023
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9067
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2935
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.