Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Regarding A Function Accepting Variable Number Of Arguments In C


  • Please log in to reply
2 replies to this topic

#1 csepraveenkumar


    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 54 posts

Posted 01 June 2012 - 08:56 AM

When we use va_list in function in c to make it accept a variable number of arguments , the first argument that we give which starts the list of arguments invariable in one form or another specifies the number or the type or both things about the arguments that are going to follow. Isn't there any way in which we don't have to specify this information before hand?
  • 0

#2 kernelcoder


    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 01 June 2012 - 09:24 AM

No -- there is no way. We have to have at-least one argument (but can be more than one) before the 3 dots. The second argument to va_start macro must be the argument before the variable arguments. Again, the arguments before the 3 dots can be of any type. There is not relation between the types of variable number of arguments and the immediate before argument to the variable arguments. The macro uses that argument just to determine where to start from for the variable number of arguments.
  • 0

#3 rocketboy9000


    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 79 posts

Posted 20 June 2012 - 09:55 AM

For any given call of the variable argument function, it must find out somehow how many arguments it has been given. If your arguments are going to mark the end, rather than the beginning of the list, try this:
#define foo(...) foo_internal(0,__VA_ARGS__);
foo_internal(int dummy,...);
Be very careful.
  • 0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download