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

Discussion in 'C++' started by Kapt. Boogschutter, Jun 19, 2004.

  1. 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
    Kapt. Boogschutter, Jun 19, 2004
    #1
    1. Advertising

  2. Kapt. Boogschutter

    Jack Klein Guest

    On Sat, 19 Jun 2004 21:50:38 +0200, "Kapt. Boogschutter"
    <> 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/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jun 19, 2004
    #2
    1. Advertising

  3. "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.
    >
    > 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
    Victor Bazarov, Jun 19, 2004
    #3
  4. "Kapt. Boogschutter" <> wrote in message
    news:cb25ec$1cs$1.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
    John Harrison, Jun 19, 2004
    #4
  5. Kapt. Boogschutter

    JKop Guest

    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);
    }
    }


    As for converting integers to strings, perhaps use the string class and have

    string* pArrayOfStrings

    Or use standard library functions.


    -JKop
    JKop, Jun 19, 2004
    #5
  6. Kapt. Boogschutter

    Rolf Magnus Guest

    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.
    Rolf Magnus, Jun 19, 2004
    #6
  7. 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?"
    edA-qa mort-ora-y, Jun 20, 2004
    #7
  8. "Kapt. Boogschutter" <> schreef in bericht
    news:cb25ec$1cs$1.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
    Kapt. Boogschutter, Jun 20, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Kial
    Replies:
    1
    Views:
    635
    Eric Enright
    Nov 15, 2004
  2. S?ren Gammelmark
    Replies:
    1
    Views:
    1,871
    Eric Sosman
    Jan 7, 2005
  3. AikidoGuy
    Replies:
    11
    Views:
    549
    Seebs
    Nov 21, 2011
  4. Vincent Arnoux
    Replies:
    1
    Views:
    236
    Arnaud Bergeron
    Aug 11, 2006
  5. Ryan Dupuis
    Replies:
    8
    Views:
    1,217
    Eric Sosman
    Oct 2, 2013
Loading...

Share This Page