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

Home Posts Topics Members FAQ

va_list usage

given this example:

void bar(const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
baz(fmt, ap);

va_end(ap);
}

void baz(const char *fmt, va_list ap)
{
char msg[MSG_SIZE];

vsnprintf(msg, sizeof msg, fmt, ap);

display(msg);
}

is this usage well defined? that is, passing a va_list
from one function and using it in another.
also, i've been wondering about something.
where in the standard is the following explained?

void func(int foo);

....

(****func)(foo) ; /* where is this method of calling func explained? */
Nov 14 '05 #1
2 2541
j0******@engine er.com (j0mbolar) writes:
given this example:

void bar(const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
baz(fmt, ap);

va_end(ap);
}

void baz(const char *fmt, va_list ap)
{
char msg[MSG_SIZE];

vsnprintf(msg, sizeof msg, fmt, ap);

display(msg);
}

is this usage well defined? that is, passing a va_list
from one function and using it in another.
Yes.
also, i've been wondering about something.
where in the standard is the following explained?

void func(int foo);

...

(****func)(foo) ; /* where is this method of calling func explained? */


6.3.2.1#4:
| A function designator is an expression that has function type. Except
| when it is the operand of the sizeof operator or the unary & operator,
| a function designator with type "function returning type" is converted
| to an expression that has type "pointer to function returning type".

6.5.3.2#4:
| The unary * operator denotes indirection. If the operand points to a
| function, the result is a function designator; [...].

6.5.2.2#1 (Function calls, Constraints):
| The expression that denotes the called function shall have type
| pointer to function returning void or returning an object type other
| than an array type.

6.5.2.2#3 (Function calls, Semantics):
| A postfix expression followed by parentheses () containing a possibly
| empty, comma-separated list of expressions is a function call. The
| postfix expression denotes the called function. The list of
| expressions specifies the arguments to the function.

The function designator `func', which has type `function returning
void', is not the operand of a `sizeof' or unary & operator, therefore
it is converted to type `pointer to function returning void'. The
innermost * operator is then applied; the result of the expression
`*func' has type `function returning void', but it is again not the
operand of a `sizeof' or unary & operator, so it is again converted to
type `pointer to function returning void'. This happens repeatedly.
Finally, the result of `(****func)' has type `pointer to function
returning void' (after conversion according to 6.3.2.1#4), and the
function is called according to 6.5.2.2#3.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #2
On 14 Apr 2004 20:52:00 -0700, j0******@engine er.com (j0mbolar) wrote:
given this example:

void bar(const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
baz(fmt, ap);

va_end(ap);
}

void baz(const char *fmt, va_list ap)
{
char msg[MSG_SIZE];

vsnprintf(msg, sizeof msg, fmt, ap);

display(msg);
}

is this usage well defined? that is, passing a va_list
from one function and using it in another.

Yes. Although to be really really pedantic, you should have a
prototype declaration of baz() in scope at (before) the call to it;
you aren't absolutely guaranteed va_list isn't a type altered by the
default argument promotions, though that's damn unlikely except on
DS9k. In C99 of course you must have some declaration, and it might as
well be prototype. And it's good >style< to always have prototype
declarations or definitions, but not required.

Also, for your particular example but not your general question,
vsnprintf is not standard before C99; though common in C90
implementations as an extension it is sometimes spelled differently or
has slightly different semantics.

If you go on to more complex cases than asked and shown, one thing
that is *not* well defined is using the same va_list in the caller
after the callee returns. In particular, it is not specified by the
Standard whether va_list is an array type and hence passed "by
reference" (that is, by decayed pointer) and shared, or a type like a
pointer or struct that is passed by value and not shared. If this is
an issue, pass an explicit pointer (and dereference) to force sharing,
or in C99 only use va_copy and a second va_list, in either the caller
or callee, to prevent it.

- David.Thompson1 at worldnet.att.ne t
Nov 14 '05 #3

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

Similar topics

6
3696
by: Peter | last post by:
-------------------------------------------------------------------------------- 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
3
16782
by: Douwe | last post by:
I try to build my own version of printf which just passes all arguments to the original printf. As long as I keep it with the single argument version everything is fine. But their is also a version which uses the "..." as the last parameter how can I pass them to the orignal printf ? void myprintf(char *txt, ...) printf(txt, ???????); }
2
3346
by: Joerg Schoen | last post by:
Hi folks! I have a function that gets a 'va_list'. I am passing the 'va_list' two times to a function like 'vprintf' to print it out. I thought that this was portable until I came across a platform (AS/400) where it fails: The 'va_list' is actually defined as some some kind of an array, so passing it happens by *reference*: The first user will destroy it and the second
1
495
by: j0mbolar | last post by:
given this example: void bar(const char *fmt, ...) { va_list ap; va_start(ap, fmt); baz(fmt, ap); va_end(ap);
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 }
11
3076
by: thierrydollar | last post by:
Hi, I have written a very simple program using variable arguments calls and I get strange things that I cannot explain. I have one function (add) that displays two parameters. It works well when I call it directly. Now I have a second function (set) that also calls add. But this doesn't
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...
2
361
by: Peter Nilsson | last post by:
Jesse Ziser <d...@spam.diewrote: The detail in VARIABLE ARGUMENTS explicitly allows it. Nothing prohibits it. Only va_start and va_end must 'be invoked in the function accepting varying number of arguments.' They might accidentally screw it up, but the semantics seem pretty clear.
1
5551
by: Chuck Chopp | last post by:
I have some code that is being built on the following: Windows Server 2003, both 32-bit & 64-bit editions Windows Vista, both 32-bit & 64-bit editions Windows Server 2008, both 32-bit & 64-bit editions Build tools: Visual Studio 2008. SLES 10 SP1, both 32-bit & 64 bit editions
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
10025
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
9068
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
6804
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5461
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4138
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
2937
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.