473,508 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to make a Function with Variable Argument List (type unknown)

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 types are specified within the
first argument.

Since this is not the case with my function I have no idea how to make this
function and make the type conversion as descriped before.

Can anyone point me in the right direction?

ThanX
Jul 22 '05 #1
7 6239
On Sat, 19 Jun 2004 21:50:38 +0200, "Kapt. Boogschutter"
<so*****@nobody.com> wrote in comp.lang.c++:
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 types are specified within the
first argument.

Since this is not the case with my function I have no idea how to make this
function and make the type conversion as descriped before.

Can anyone point me in the right direction?

ThanX


Let me get this straight. You are trying to write a function that can
accept one or more arguments. These arguments are all strings, and
the first string does not contain information about how many other
strings there might be. Is that correct?

In that case, if I call:

your_function("one");

your_function("one", "two");

your_function("one", "two", "three");

....how are you going to write your function so that when it sees "one"
it knows whether or not there is a "two" or a "three"?

The standard header <cstdarg> provides a mechanism for a function to
access a variable argument list, but there must be a mechanism for it
to determine what the arguments are (how many and what type). If you
don't want the information in the arguments before the variable ones
to tell you, you must use some kind of special end item.

If your function must be called like this:

func("one", (char *)0);

func("one", "two", (char *)0);

func("one", "two", "three", (char *)0);

....with the null pointer indicating the end of the list, you can still
use <cstdarg.h>.

For sure your function must have some way of knowing how many
parameters are passed, because if it tries to go past the number you
get undefined behavior, and probably a crashed program.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #2
"Kapt. Boogschutter" <so*****@nobody.com> wrote...
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 types are specified within the
first argument.

Since this is not the case with my function I have no idea how to make this function and make the type conversion as descriped before.
If specifying the number of arguments [implicitly] is not the case with
your function, how the hell will it know how many arguments were passed
to it?
Can anyone point me in the right direction?


Not until you give a more specific description of what you're trying to
accomplish.

V
Jul 22 '05 #3

"Kapt. Boogschutter" <so*****@nobody.com> wrote in message
news:cb**********@news4.tilbu1.nb.home.nl...
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".
That is impossible in C++.

My problem is that I can only find samples and description of printf() like functions where the optional arguments and types are specified within the
first argument.

Since this is not the case with my function I have no idea how to make this function and make the type conversion as descriped before.
Well I have no idea how you intend to work out how many arguments there are,
it must be specified by the caller in some manner.

Can anyone point me in the right direction?


I think you are asking too much. What is this function going to do exactly?

john
Jul 22 '05 #4
Kapt. Boogschutter posted:
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 types are specified
within the first argument.

Since this is not the case with my function I have no idea how to make
this function and make the type conversion as descriped before.

Can anyone point me in the right direction?

ThanX


What a disgusting function.
I myself would go with something like the following:
int Pig(unsigned char amount_strings, char** pArrayOfPointersToStrings)
{
for(unsigned char i = 0; i < amount_strings; i += 1)
{
WantsString(pArrayOfPointersToStrings[i]);
}
}
As for converting integers to strings, perhaps use the string class and have

string* pArrayOfStrings

Or use standard library functions.
-JKop
Jul 22 '05 #5
Kapt. Boogschutter wrote:
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 types are specified
within the first argument.


That's for a reason. You can write functions with variable argument
lists, but a lot of the usual C++ functionality will be gone. C++ won't
give your function any hint about how many arguments were given or
which type they are. Your function will "just have to know", which
means you have to give that information explicitly to the function.
Further, C++ won't do any automatic conversion if you're trying to
interpret a parameter as another type than it acutally is, and you'll
receive no warning or error message. Usually, it will result in a crash
if you're lucky. And you can only pass POD types as variable arguments.
Basically, better avoid variable argument lists.

Jul 22 '05 #6
Kapt. Boogschutter wrote:
Since this is not the case with my function I have no idea how to make this
function and make the type conversion as descriped before.
Can anyone point me in the right direction?


You are probably looking for a more natural way to specify the list of
strings. In which case, there is probably and okay solution, which I've
seen work for templates before.

You can create a custom type, say StringBuffer, which overloads the ','
comma operator and has an implicit CTOR that takes (const char*). The
comma operator could then append a string to the StringBuffer. A
function something like this

StringBuffer& operator,( const char* str ) {
vector.push_back( str );
return *this;
}

Then your target function takes one parameter of type StringBuffer

void function( StringBuffer& buf ) ...

But you'll need to call your function like this:

function( ((StringBuffer)"one", "two", "three" ) );
function( ((StringBuffer)"one", "four" ) );
function( "one" ); //this case works from implicit conversion

Perhaps you can clean up the syntax, but they may make your program
less clear.

Attached is a working example.

--
edA-qa mort-ora-y (Producer)
Trostlos Records <http://trostlos.org/>

"What suffering would man know if not for his own?"

Jul 22 '05 #7

"Kapt. Boogschutter" <so*****@nobody.com> schreef in bericht
news:cb**********@news4.tilbu1.nb.home.nl...
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 types are specified within the
first argument.

Since this is not the case with my function I have no idea how to make this function and make the type conversion as descriped before.

Can anyone point me in the right direction?

ThanX

ThanX everyone for the insigth,

Your answers give me a little bit more to go on with.
I had hoped that I could get the length (number of arguments) of the list
with some kind of sizeof() function.
The Idea of using an array of JKop is interesting but since the size is
determined run-time I must use vectors instead.

ThanX everybody
Jul 22 '05 #8

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

Similar topics

18
2077
by: Razvan | last post by:
Hi! What is the purpose of such a function ? int function(void)
4
2095
by: Ole | last post by:
hello, Little problem: struct operatable { char * operatable_id; int ( * delegate ) ( ... ); somedatatype data; };
17
3571
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
6
4841
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
19
2411
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
1
7270
by: xahlee | last post by:
Elisp Tutorial: Make Google Earth Xah Lee, 2006-12 This page shows a example of writing a emacs lisp function that creates a Google Earth file, and creates a link to the file, as well a link...
5
4644
by: kris | last post by:
Hi I have written a program which prints the hostid on a linux system. The programm uses gethostid() function call which is defined in the library file unistd.h. But my programm gets compiled...
30
2694
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;
1
47348
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
7223
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
7115
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
7377
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...
1
7036
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
5624
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,...
1
5047
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...
0
3191
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
762
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.