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

reading <stdarg.h>'s va_list twice

I'd like to be able to scan a va_list twice in a v... function, but
can't see how to do it. For example

char *strdupcat(const char *first, ...)
{
char *result, pos;
va_list arg;
size_t len;
const char *next;

va_start(arg, first);
next = first;
while (next) {
len += strlen(next);
next = va_arg(arg, const char *);
}
va_end(arg);

result = pos = malloc(len + 1);

va_start(arg, first);
next = first;
while (next) {
strcpy(pos, next);
pos += strlen(pos);
next = va_arg(arg, const char *);
}
va_end(arg);
*pos = 0;

return result;
}

This should be fine (it's a close paraphrase, but I haven't tried it).
It concatenates string arguments on the argument until one which is
NULL is reached. Now I want to have a function (analogous to vprintf
for printf) that does the same thing called from another variadic
function - how do I handle the rewinding done by va_end/va_start in a
portable way, given that I won't have "first" again? In other words,
how can I write (portably) vstrdupcat in:

char *choseThenConcat(int which, const char *first, ...)
{
char *res;
/* do something with which, then */
va_list args;
va_start(args, first);
res = vstrdupcat(first, args);
va_end();
/* do stuff with res */
return ...something-or-other...
}
Oct 21 '08 #1
9 5430
to**********@gmail.com writes:
I'd like to be able to scan a va_list twice in a v... function, but
can't see how to do it.
<snip>
how do I handle the rewinding done by va_end/va_start in a
portable way, given that I won't have "first" again? In other words,
how can I write (portably) vstrdupcat in:

char *choseThenConcat(int which, const char *first, ...)
{
char *res;
/* do something with which, then */
va_list args;
va_start(args, first);
res = vstrdupcat(first, args);
va_end();
/* do stuff with res */
return ...something-or-other...
}
va_copy

--
Ben.
Oct 21 '08 #2


Ben Bacarisse wrote:
to**********@gmail.com writes:
I'd like to be able to scan a va_list twice in a v... function, but
can't see how to do it.
<snip>

va_copy
Where would you put the va_copy?
And for pre-C99? I want this to work with C89, if at all possible.

Tony
Oct 21 '08 #3
to**********@gmail.com writes:
Ben Bacarisse wrote:
>to**********@gmail.com writes:
I'd like to be able to scan a va_list twice in a v... function, but
can't see how to do it.
<snip>

va_copy

Where would you put the va_copy?
Inside a function that takes a va_list you can do this:

void example(va_list alist)
{
va_list copy_of_alist;
va_copy(copy_of_alist, alist);
/* process original alist */
/* process copy_of_alist */
va_end(alist);
va_end(copy_of_alist);
}
And for pre-C99? I want this to work with C89, if at all possible.
Ah. In that case I break you concatenation function into two. One to
whatever the first pass does (no doubt count the strings and their
lengths) and another to do the concatenation.

In fact, I'd be inclined do this anyway.

--
Ben.
Oct 21 '08 #4


Ben Bacarisse wrote:
to**********@gmail.com writes:
And for pre-C99? I want this to work with C89, if at all possible.

Ah. In that case I break you concatenation function into two. One to
whatever the first pass does (no doubt count the strings and their
lengths) and another to do the concatenation.

In fact, I'd be inclined do this anyway.

--
Ben.
I was afraid you'd say that... but thanks anyway!
Oct 21 '08 #5
Ben Bacarisse <be********@bsb.me.ukwrites:
to**********@gmail.com writes:
>[va_copy]
And for pre-C99? I want this to work with C89, if at all possible.

Ah. In that case I break you concatenation function into two.
If you are willing to give up strict portability, then you may
find that the following (or similar; I have not carefully
proofread it) is good enough:

#include <stdarg.h>
#ifndef va_copy
# ifdef __va_copy
# define va_copy(a, b) __va_copy(a, b)
# else
# define va_copy(a, b) ((a) = (b))
# endif
#endif
In that case I break you concatenation function into two. One to
whatever the first pass does (no doubt count the strings and their
lengths) and another to do the concatenation.
Of course, that will definitely work.
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming
Oct 21 '08 #6
Ben Bacarisse <ben.use...@bsb.me.ukwrote:
...
void example(va_list alist)
{
* * va_list copy_of_alist;
* * va_copy(copy_of_alist, alist);
* * /* process original alist */
* * /* process copy_of_alist */
* * va_end(alist);
No, va_end should be applied to alist in the function
that initialised alist with va_start or va_copy.
* * va_end(copy_of_alist);

}
And for pre-C99? I want this to work with C89, if at
all possible.

Ah. *In that case I break you concatenation function
into two. *One to whatever the first pass does (no
doubt count the strings and their lengths) and another
to do the concatenation.
Or pass two va_lists.
In fact, I'd be inclined do this anyway.
--
Peter
Oct 21 '08 #7
Peter Nilsson <ai***@acay.com.auwrites:
Ben Bacarisse <ben.use...@bsb.me.ukwrote:
>...
void example(va_list alist)
{
Â* Â* va_list copy_of_alist;
Â* Â* va_copy(copy_of_alist, alist);
Â* Â* /* process original alist */
Â* Â* /* process copy_of_alist */
Â* Â* va_end(alist);

No, va_end should be applied to alist in the function
that initialised alist with va_start or va_copy.
Yes, thanks. I don't know how that got in there.
>Â* Â* va_end(copy_of_alist);

}
And for pre-C99? I want this to work with C89, if at
all possible.

Ah. Â*In that case I break you concatenation function
into two. Â*One to whatever the first pass does (no
doubt count the strings and their lengths) and another
to do the concatenation.

Or pass two va_lists.
I am probably too old to say it but I am thinking "sweet!" :-) (and
also "why didn't I think of that?").

--
Ben.
Oct 21 '08 #8
tonybalin...@gmail.com wrote:
I'd like to be able to scan a va_list twice in a v...
function, but can't see how to do it. For example

* char *strdupcat(const char *first, ...)
* {
You don't have to scan it twice...

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

static
char *vzjoin(size_t z, const char *head, va_list ap)
{
char *r;

if (head == 0)
{
r = malloc(z + 1);
r[z] = 0;
}
else
{
size_t hz = strlen(head);
char *tail = va_arg(ap, char *);
r = vzjoin(z + hz, tail, ap);
memcpy(r + z, head, hz);
}

return r;
}

char *vjoin(const char *head, va_list ap)
{
return vzjoin(0, head, ap);
}

char *join(const char *head, ...)
{
char *r;
va_list ap;

va_start(ap, head);
r = vjoin(head, ap);
va_end(ap);

return r;
}

--
Peter
Oct 22 '08 #9
Ben Pfaff <bl*@cs.stanford.eduwrites:
Ben Bacarisse <be********@bsb.me.ukwrites:
to**********@gmail.com writes:
[va_copy]
And for pre-C99? I want this to work with C89, if at all possible.
Ah. In that case I break you concatenation function into two.

If you are willing to give up strict portability, then you may
find that the following (or similar; I have not carefully
proofread it) is good enough:

#include <stdarg.h>
#ifndef va_copy
# ifdef __va_copy
# define va_copy(a, b) __va_copy(a, b)
# else
# define va_copy(a, b) ((a) = (b))
# endif
#endif
It's probably better to use memcpy() rather than assignment,
in case the type involved is an array type (and __va_copy
isn't available, even though it "hopefully will be").

Nov 10 '08 #10

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

Similar topics

14
by: Neil Zanella | last post by:
Hello, I would like to ask how come the design of C++ includes std::pair. First of all I don't think many programmers would use it. For starters, what the first and second members are depends...
2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
2
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script...
11
by: Scott Brady Drummonds | last post by:
Hi, everyone, I've checked a couple of on-line resources and am unable to determine how reinterpret_cast<> is different from static_cast<>. They both seem to perform a compile-time casting of...
14
by: Dylan Nicholson | last post by:
Been playing around with this all day, and haven't found a solution I like yet. Assuming some initial function: void foo(std::string& src) { src += "some fixed string"; src += bar(); src...
10
by: Szabolcs Horvát | last post by:
Consider the attached example program: an object of type 'A' is inserted into a 'map<int, Am;'. Why does 'm;' call the copy constructor of 'A' twice in addition to a constructor call? The...
3
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt,...
14
by: Michael | last post by:
Since the include function is called from within a PHP script, why does the included file have to identify itself as a PHP again by enclosing its code in <?php... <?> One would assume that the...
35
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
0
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,...

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.