Thread: Variable Argument functions

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    38

    Variable Argument functions

    I have a question about Variable size argument functions. When using printf() a variable argument size function you do not need to give the number of arguments in the function call but when creating your own you must pass the number of arguments in the list. Is it possible to not pass the number of arguments like printf()? Thanks

  2. #2
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    printf() is used in C; this is the C++ forum. You might have better results in the correct forum.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    38
    I was just using printf() as an example. This is still a C++ question. Please move back to the C++ forum.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Whilst it is possible, you need to be aware that variable arguments are not type safe in any way.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by Sentral
    printf() is used in C; this is the C++ forum. You might have better results in the correct forum.
    While I agree that this question might suit better the C forum. I want to point out that it is possible and not bad at all to use printf() in C++. Sometimes, it's simply much easier without having any drawbacks.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think the C++ solution would be to use operator chaining with some suitable class type. In the case of the equivalent to printf(), we see it used in an overloaded operator<< returning a reference to the output stream that was passed to it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    Why do so much work ? It is possible to reproduce the exact same behaviour without that much crap. This is what you are looking for, my friend.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    38
    Quote Originally Posted by Desolation
    Why do so much work ? It is possible to reproduce the exact same behaviour without that much crap. This is what you are looking for, my friend.
    That is why I was trying to use. I'm really confused now. Could you please explain this part. Why is fmt a pointer and how does the while loop know when to end? Thanks

    Code:
    while (*fmt)
                    switch(*fmt++) {
                    case 's':                       /* string */
                            s = va_arg(ap, char *);
                            printf("string %s\n", s);
                            break;
                    case 'd':                       /* int */
                            d = va_arg(ap, int);
                            printf("int %d\n", d);
                            break;
                    case 'c':                       /* char */
                            /* need a cast here since va_arg only
                               takes fully promoted types */
                            c = (char) va_arg(ap, int);
                            printf("char %c\n", c);
                            break;
                    }

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    There's an interesting tutorial here .

    I came by this one actually today, after an interesting conversation on another forum about macros.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Desolation
    Why do so much work ? It is possible to reproduce the exact same behaviour without that much crap. This is what you are looking for, my friend.
    Well, since this was already suggested, I will quote the exact reason you should do so much work. This quote, you can find right in this post.
    Whilst it is possible, you need to be aware that variable arguments are not type safe in any way.
    I personally agree with Laserlight. The proper C++ way would be overloaded operators to chain. Unfortnately, you may have to encapsulate Plain Old Datatypes in order to use it.
    Sent from my iPadŽ

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    38
    Quote Originally Posted by Mario F.
    There's an interesting tutorial here .

    I came by this one actually today, after an interesting conversation on another forum about macros.
    Thanks will read tonight.

    BTW how often are VA function used and how useful are they? In all my C++ classes in college this has never been mentioned. I'm trying to learn some things on my own.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    VA functions can be very powerful, and you will know when you need one. Reasons exist why they aren't seen to often; most things that would be VA functions can work efficiently a different way. For instance, you could implement a sort function that takes a lot of arguments for items to sort, but then why not put those items in a container? Containers usually save a programmer inumerable headaches.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by dnysveen
    BTW how often are VA function used and how useful are they?
    Unspecified (is a better term, i reckon - Stroutstrup 3rd edition - Variable argument functions are dealt with by function overloading and default arguments) argument functions are only needed when both the number of the arguments and the type of the arguments vary, and they vary in an unspecified way only known at run-time.

    This is a rare situation. So rare in fact that entire programming languages there are that don't offer this possibility. And on those few cases in which one can think of some use, if the possible variations are somewhat limited to a manageable number, it's preferable to simply overload the function and create a "sidekick" function that deals with the common bits to make code more easy to maintain. Or use containers, as Citizen suggested.

    The major reason why you should always consider overload over the elipsis has already been given by Laserlight. However, truth be told, dangerous options are usually the most publicized. So I'm tempted to think the reason why you don't hear much about these functions is because they are very rare.
    Last edited by Mario F.; 06-01-2006 at 06:05 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 07-26-2007, 01:08 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  4. I'll have a Variable argument list to go...
    By SMurf in forum C Programming
    Replies: 6
    Last Post: 02-27-2003, 02:02 PM
  5. rerouting variable argument lists...
    By doubleanti in forum C++ Programming
    Replies: 9
    Last Post: 12-14-2001, 09:28 AM