473,804 Members | 3,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(cons t 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 *choseThenConca t(int which, const char *first, ...)
{
char *res;
/* do something with which, then */
va_list args;
va_start(args, first);
res = vstrdupcat(firs t, args);
va_end();
/* do stuff with res */
return ...something-or-other...
}
Oct 21 '08 #1
9 5468
to**********@gm ail.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 *choseThenConca t(int which, const char *first, ...)
{
char *res;
/* do something with which, then */
va_list args;
va_start(args, first);
res = vstrdupcat(firs t, args);
va_end();
/* do stuff with res */
return ...something-or-other...
}
va_copy

--
Ben.
Oct 21 '08 #2


Ben Bacarisse wrote:
to**********@gm ail.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**********@gm ail.com writes:
Ben Bacarisse wrote:
>to**********@gm ail.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**********@gm ail.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**********@gm ail.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...@gm ail.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(cons t 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.stanfor d.eduwrites:
Ben Bacarisse <be********@bsb .me.ukwrites:
to**********@gm ail.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
45874
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 on what you are using the pair for. For instance if I am using coordinates in two dimensional space then I like to use x and y. So I might as well define my own struct with x and y members in it and create a constructor so
2
3237
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 writing one object in C# which will take the entire table tag contents and renders. Ie., we need to pass "<table>………… <thead>……</thead>. <tr>.<td> <td>..<tr>.<td> <td> </table>" content to
2
10572
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 type="text/javascript"> <!]> </script> <script type="text/javascript"
11
5162
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 one type to another. However, I'm certain that there is something else that is happening. Can someone explain the difference or recommend an online site that can explain it to me?
14
4124
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 += "some other fixed string";
10
2979
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 constructors and copy constructors in 'A' report when they are called. 'whoami' is just a unique identifier assigned to every object of type 'A'. The output of the program is: constructor 0 constructor 1
3
3386
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, &gt ,&ltCompany&gt to display '<', '>' and '<Company>' respectively. But is there any freeware code available which could implement the above functionality without having to use &gt,&lt and such stuff???
14
3167
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 PHP interpreter works like any other, that is, it first expands all the include files, and then parses the resulting text. Can anyone help with an explanation? Thanks, M. McDonnell
35
5900
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 list. That part makes a reasonable amount of sense, as you can't actually take items away from an Array. However, there doesn't seem to be a way to perform the same operation in one fell swoop on a List<>. For example:
0
9706
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
9579
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
10330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
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
9144
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
5520
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
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
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.