Newbie question about Variable Argument Lists for Functions

This is a discussion on Newbie question about Variable Argument Lists for Functions within the C++ Programming forums, part of the General Programming Boards category; If you have a function which can accept a variable number of arguments, how can you find out how many ...

  1. #1
    Edo
    Edo is offline
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,602
    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
    21,717
    Incidentally, are you sure that you need variable argument lists in the first place?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Edo
    Edo is offline
    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
    Edo
    Edo is offline
    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
    839
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,602
    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
    839
    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, 02:21 AM
  3. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 01: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, 08:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21