Thread: Newbie question about Variable Argument Lists for Functions

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    8

    Newbie question about Variable Argument Lists for Functions

    If you have a function which can accept a variable number of arguments, how can you find out how many arguments were passed to it?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Either manually (passing number of arguments as an argument), or by fetching argument by argument until you come across a "known value" that is used to mark the end of arguments (ie NULL), or if the arguments are all of the same type, you can use a vector and use its .size() method.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Incidentally, are you sure that you need variable argument lists in the first place?
    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

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    8
    Awesome, thanks. And thanks for replying with the "safe delete" function in my other thread, too!

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    8
    Quote Originally Posted by laserlight View Post
    Incidentally, are you sure that you need variable argument lists in the first place?
    Nope, but that's where I am in the tutorial, ahaha
    Give me a little time... I'm still in the crawl stage of "crawl, walk, run"!

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by Elysia View Post
    Either manually (passing number of arguments as an argument), or by fetching argument by argument until you come across a "known value" that is used to mark the end of arguments (ie NULL), or if the arguments are all of the same type, you can use a vector and use its .size() method.
    it's probably worth mentioning that you cannot use differing types in va lists without some additional effort/sophistication either, so using a vector is generally a good choice.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Unless the function expects different types, in order. But generally, you are right, of course.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    yes, that is true if there is an assumed repeating pattern to the types passed. however it may well still be preferable, even if arguably marginally less convenient, to pass a vector of structs.

    just yesterday i made a polynomial function that uses a variable argument constructor, where the constructor arguments go: unsigned int order, int exp1,double coeff1,...int expn,double coeffn

    and what did i end up having to do immediately thereafter?

    declaring
    Code:
    struct ecpair{int exp;double coeff;}
    within the body of polynomial to store the data in a vector. in then end it would have been vetter to just pass a vector<polynomial::ecpair>

    the only thing i can really see variable argument functions providing unique functionality is is where the first argument is a typelist, as then there need be no pattern, but then my compiler doesn't match the function so it doesn't work anyway. :\

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  2. Newbie question on Structures
    By Gatt9 in forum C++ Programming
    Replies: 15
    Last Post: 03-26-2005, 03:21 AM
  3. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  4. global variable and header question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2002, 11:38 PM
  5. rerouting variable argument lists...
    By doubleanti in forum C++ Programming
    Replies: 9
    Last Post: 12-14-2001, 09:28 AM