473,666 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creative uses for variable argument list functions

Every time I've seen an example of a variable argument list
function its functionality was to print formatted output. Does
anyone have examples where the function is not some variation
of printf ?

Jul 30 '07 #1
19 2122
Spiros Bousbouras said:
Every time I've seen an example of a variable argument list
function its functionality was to print formatted output. Does
anyone have examples where the function is not some variation
of printf ?
What a curious question! Well, let's see...

It's easy to come up with examples, of course - but rather harder to
come up with examples that might actually be useful. For example, one
might calculate a sum using 0 as a terminator (since 0 doesn't affect
the sum, this is not unreasonable), but why bother? One might just as
easily write a routine that sums the first n items in an array, which
would be far more useful.

I suspect that, if there is a good answer to your question, it might lie
in the realms of expression-parsing or something along those lines.

I look forward to reading other answers, hopefully from people who are
thinking more creatively than me today.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 30 '07 #2
In article <xI************ *************** ***@bt.com>,
Richard Heathfield <rj*@see.sig.in validwrote:
>Spiros Bousbouras said:
>Every time I've seen an example of a variable argument list
function its functionality was to print formatted output. Does
anyone have examples where the function is not some variation
of printf ?

What a curious question! Well, let's see...

It's easy to come up with examples, of course - but rather harder to
come up with examples that might actually be useful. For example, one
might calculate a sum using 0 as a terminator (since 0 doesn't affect
the sum, this is not unreasonable), but why bother? One might just as
easily write a routine that sums the first n items in an array, which
would be far more useful.

I suspect that, if there is a good answer to your question, it might lie
in the realms of expression-parsing or something along those lines.

I look forward to reading other answers, hopefully from people who are
thinking more creatively than me today.
The one time I used this was to write a "max" function, that would
return the maximum value from a list of numbers.

Jul 30 '07 #3
Spiros Bousbouras wrote:
>
Every time I've seen an example of a variable argument list
function its functionality was to print formatted output. Does
anyone have examples where the function is not some variation
of printf ?
How about a version of strcat() that takes a list of strings, which
could be called like this:

StrCatList(MyBu ffer,MyBufferLe n,string1,strin g2,string3,NULL );

Yes, one could use sprintf() for this, but strcat-like functionality
is (most likely) more efficient.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Jul 30 '07 #4
On Jul 30, 12:38 pm, Spiros Bousbouras <spi...@gmail.c omwrote:
Every time I've seen an example of a variable argument list
function its functionality was to print formatted output. Does
anyone have examples where the function is not some variation
of printf ?
I once wrote a variable argument string concatenation function that
returned a new string consisting of a catenation of all the provided
strings. The variable argument facility is useful is when you need to
create a function that can operate on a variable number of items in
which it would be inconvenient or impossible to supply the items as an
array. Examples include the execl* functions in POSIX and functions
that accept placeholders for a prepared statement in a SQL API.

Robert Gamble

Jul 30 '07 #5
Kenneth Brody said:
Spiros Bousbouras wrote:
>>
Every time I've seen an example of a variable argument list
function its functionality was to print formatted output. Does
anyone have examples where the function is not some variation
of printf ?

How about a version of strcat() that takes a list of strings, which
could be called like this:

StrCatList(MyBu ffer,MyBufferLe n,string1,strin g2,string3,NULL );

Yes, one could use sprintf() for this, but strcat-like functionality
is (most likely) more efficient.
Since it's my day for being dense, I suppose I might as well attract
some flak for messing up the implementation:

#include <stdarg.h>
#include <stddef.h>

int catstrlist(char *buf, size_t len, ...)
{
int rc = 1; /* insufficient memory */
const char *arg = "";

va_list ap = {0};

va_start(ap, len);

while(len 1 && arg != NULL)
{
if(*arg == '\0')
{
arg = va_arg(ap, const char *);
}
else
{
*buf++ = *arg++;
--len;
}
}
*buf = '\0';
if(arg == NULL)
{
rc = 0; /* all done successfully */
}

va_end(ap);
return rc;
}
/* driver */
#include <stdio.h>
#include <string.h>

int main(void)
{
int rc = 0;
char target[32] = {0};

rc = catstrlist(targ et,
sizeof target,
"Hello", "World", NULL);
printf("Expecte d [HelloWorld], "
"got [%s]: Expected 0, got %d\n", target, rc);

memset(target, 0, sizeof target);
rc = catstrlist(targ et,
sizeof target,
"Now", "Is", "The", "Hour", NULL);
printf("Expecte d [NowIsTheHour], "
"got [%s]: Expected 0, got %d\n", target, rc);

memset(target, 0, sizeof target);
rc = catstrlist(targ et,
sizeof target,
"WayTooManyInpu tCharacters",
"In", "This", "Sequence", NULL);
printf("Expecte d [WayTooManyInput CharactersInThi s], "
"got [%s]: Expected 1, got %d\n", target, rc);

return 0;
}

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 30 '07 #6
Kenneth Brody wrote On 07/30/07 13:33,:
Spiros Bousbouras wrote:
>>Every time I've seen an example of a variable argument list
function its functionality was to print formatted output. Does
anyone have examples where the function is not some variation
of printf ?


How about a version of strcat() that takes a list of strings, which
could be called like this:

StrCatList(MyBu ffer,MyBufferLe n,string1,strin g2,string3,NULL );
Side-issue: Question 5.2 in the comp.lang.c Frequently
Asked Questions (FAQ) list <http://www.c-faq.com/explains
why this is *not* the right way to call such a function.

To the original point: The variable argument list is
best avoided except when it's unavoidable, because it offers
so many opportunities for error. The principal difficulty
is that the compiler does not know your conventions for the
arguments, and thus can't warn when you violate your own
rules accidentally -- as above, for example. You get the
dubious pleasure of having your code compile successfully
and then chasing down the undefined behavior at leisure.

That, I think, does a lot to explain the relative rarity
of varargs. It's more or less unavoidable in I/O (where it
gives you not only the ability to pass argument lists of
varying lengths, but also of varying composition), but in
other settings people usually try to make their interfaces
less error-prone by finding ways to use fixed argument lists.

--
Er*********@sun .com
Jul 30 '07 #7
dan
On Jul 30, 8:38 am, Spiros Bousbouras <spi...@gmail.c omwrote:
Every time I've seen an example of a variable argument list
function its functionality was to print formatted output. Does
anyone have examples where the function is not some variation
of printf ?
I got thinking the same thing recently. Can variable argument list
functions be used to make a function able to handle int, long, or
float, depending on what is passed? As a simple example, a MAX
function, as someone else mentioned, that could deal with different
number types.

Is yes, would that be a good idea, or just better to write separate
functions?

Daniel Goldman

Jul 30 '07 #8

"Spiros Bousbouras" <sp****@gmail.c omwrote in message
news:11******** *************@b 79g2000hse.goog legroups.com...
Every time I've seen an example of a variable argument list
function its functionality was to print formatted output. Does
anyone have examples where the function is not some variation
of printf ?
One example that comes to mind is on Unix/Linux using Xt functions
such as XtVaSetValues( object, ..., NULL )
where ...,NULL represents a NULL-terminated list of
resource name/value pairs to set on the specified object.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing
Jul 30 '07 #9
In article <11************ **********@d30g 2000prg.googleg roups.com>,
dan <da*******@yaho o.comwrote:
>I got thinking the same thing recently. Can variable argument list
functions be used to make a function able to handle int, long, or
float, depending on what is passed? As a simple example, a MAX
function, as someone else mentioned, that could deal with different
number types.
Yes, but keep in mind that the mechanisms have no inherent way
to find out what the type of the passed variable was, and so must
be told (and so must hope that the teller didn't accidently
misrepresent the types.) For example, you could have

GenericOutputTy pe MYMAX(argtypein dicator, ...)

GenericOutputTy pe perhaps being void* or a union type -- something
allowing you to return different types depending on the inputs.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Jul 30 '07 #10

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

Similar topics

2
7908
by: Suzanne Vogel | last post by:
'stdarg.h' defines the 'va_arg' type to use in passing variable numbers of parameters to functions. The example of its use given in the Dinkumware documentation *seems* to imply that the 'va_arg' list should *automatically* be terminated with NULL past the user's last argument: http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=stdarg.html However, I (for some reason) have to pass in the count of the number of arguments, or manually...
7
6276
by: Kapt. Boogschutter | last post by:
I'm trying to create a function that has at least 1 Argument but can also contain any number of Arguments (except 0 because my function would have no meaning for 0 argument). The arguments passed to the function are strings or must be (automaticly converted to a string e.g. the number 10 should become the string "10". My problem is that I can only find samples and description of printf() like functions where the optional arguments and...
10
5015
by: The Directive | last post by:
I read the C FAQ question on passing a variable number of arguments, but it didn't help. The example assumes all arguments are of the same type. I want to create a function "trace" that can be used like this: trace( "Err", errtType, lineNum, NULL) /* where errType is char* and lineNum is an int */
5
411
by: matevz bradac | last post by:
Hi, I'm trying to implement delayed function execution, similar to OpenGL's display lists. It looks something like this: beginList (); fcn1 (); fcn2 ();
1
2199
by: S?ren Gammelmark | last post by:
Hi I have been searching the web and comp.lang.c of a method of using variable-argument function pointers and the like. And some of the questions arising in this post are answered partly in these posts, but I ask mainly for a way to found a way to solve this problem. I'm a hobbyist so there might be an alltogether better solution to the problem, so please come with any solution you might think of. I'm open to ideas. --- And i'm not...
1
394
by: Nimmi Srivastav | last post by:
Consider two functions A and B, both of which accept a variable number of arguments (va_start, va-arg, va_end). Is there an easy way for arguments passed to A to be, in turn, passed to B? (For example, if A is a wrapper function around B). Thanks, Nimmi
3
4275
by: Tomás | last post by:
Let's say we have a variable length argument list function like so: unsigned GetAverage(unsigned a, unsigned b, ...); I wonder does the compiler go through the code looking for invocations of the function, and from there, treat it sort of like a template function? For instance, if we had a template function like so: template<class T> T GetAverage(T a, T b);
6
3206
by: CptDondo | last post by:
How do you declare a function with 0 or mroe arguments? I have a bunch of functions like this: void tc_cm(int row, int col); void tc_do(void); void tc_DO(int ln); and I am trying to declare a pointer to them:
30
2725
by: Adam | last post by:
Hi, I have a simple printf-like function: int print(const char *format, ...) { char buffer; va_list argptr; int i;
0
8440
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
8355
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
8781
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
8550
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
8638
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...
1
6191
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4365
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
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
2
2006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.