473,320 Members | 1,861 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,320 software developers and data experts.

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 2504
j0******@engineer.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******@engineer.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.net
Nov 14 '05 #3

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

Similar topics

6
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...
3
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...
2
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...
1
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
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)); ...
11
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...
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...
2
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...
1
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.