function with variable number of parameters

This is a discussion on function with variable number of parameters within the C++ Programming forums, part of the General Programming Boards category; Hi, I realized that some functions can take an undefined number of parameters, and I was wondering how I could ...

  1. #1
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114

    function with variable number of parameters

    Hi, I realized that some functions can take an undefined number of parameters, and I was wondering how I could achieve that. To give an example, printf() can have a variable lenght of parameters, and if you look at its prototype, it looks like this:
    Code:
    int printf(const char*, ...)
    So I decided to "try" to build a function that would accept as many parameters as possible. So I could write this, but after that, I still don't know how to deal with the parameters!
    Code:
    void func(...)
    {
        //???
    }
    I know that I can't try with "...[0]" for accessing a parameter, but there has to be a way...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    You need to read up on the va_start, va_arg and va_end in stdarg.h

    Also, you need at least ONE fixed parameter to be able to handle variable arguments.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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 mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Thanks! I will study this!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Variable number of arguments in function.
    By kamoda_pawel in forum C Programming
    Replies: 1
    Last Post: 01-18-2007, 07:18 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  5. A function accepting a variable number of arguments.
    By Lithorien in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2004, 02:48 AM

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