Thread: function(...) --- how do i refer to variables?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    17

    function(...) --- how do i refer to variables?

    hi guys!

    i have problem. if i use a function with this prototype

    Code:
    void function (...)
    how can i refer to it's arguments, with in the function?

  2. #2
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    Ignoring that the prototype you gave is illegal because you need at least one non-variable argument--to know where the variable arguments start--you can access the variable arguments with the macros in stdarg.h:
    Code:
    #include <stdio.h>
    #include <stdarg.h>
    
    void func ( int n, ... )
    {
      va_list args;
      int sum = 0;
    
      /* Initialize the variable argument list */
      va_start ( args, n );
    
      while ( --n >= 0 )
        sum += va_arg ( args, int );
      printf ( "Total sum: %d\n", sum );
    
      /* Clean up the variable argument list */
      va_end ( args );
    }
    
    int main ( void )
    {
      func ( 5, 1, 2, 3, 4, 5 );
    
      return 0;
    }

  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
    Jan 2006
    Posts
    17

    for Dave_Sinkula and Slacker

    Thanks to both of you ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM