• C

How do I specify a variable number of parameters to a funciton?

For example, I have the following function:

void openFiles(const char *fileName,...)

This function may have passed any number of parameters, so I cannot simply name them 'fileName1, fileName2, etc.'  I already know that  the (fileName, ...) is acceptable for a prototype.  However, I don't know how to access any arguments that are passed in beyond fileName.

Who is Participating?
The idea is to use the address of the parameters. They are passed through the stack, and appear in sequential order.
Please see details in the comment below.
Passing parameters to functions is performed through the stack. This is all handled through the compiler, so you don't really have to know anything about it, but you need to know how it works to fiddle around with things like variable parameter lists.

Parameters are passed to the stack in the following order in the C calling convention (as opposed to Pascal, which is the other way around): The last parameter is pushed first, then the next-to-last, and so on. So, the way to access the next parameter after fileName is to take fileName's address and then move to the previous address. Now, this means that you will have to know the exact size of the other arguments, as well as their data type. (This is why functions like printf have a string which specifies the type and number of parameters.)

For example, let's say that you want to access the parameter immediately after fileName, and that that parameter is an int. Here is what you do:

// ... fileName is a parameter
int * pNextParam = (int*)(void*)&fileName;
pNextParam--; // move pointer back, to previous parameter
int nNextParam = * pNextParam;
// next parameter stored in nNextParam

Please note that on some systems, the stack grows downwards (i.e. previous elements are found in higher memory areas, not lower memory areas as depicted in the above code segment, so that the -- above may need to be changed to ++. This is architecture-dependent, that is, generally all x86 systems will have one convention, all Macs will have a different convention and so on. I believe the above code will work for x86 systems - perhaps someone can verify this?

If you want more information about the C calling convension, visit http://www.mcs.vuw.ac.nz/comp/Courses/203/1997/Labs/Lab2/

Good luck!
-- Slarti
Or if you want to be portable, use the ANSI header <stdarg.h>
and the macros va_start(va_list ap, fileName), va_arg(va_list ap, type), va_end(ap)

Please not e that Slarti's solution will not work with arguments of different sizes (not to mention being extremely unportable, thus having good chances of not working at all on your compiler of choice).  Ozo's comment provides a *standard* way of doing it.

Here's an example:

    #include <stdio.h>
    #include <stdarg.h>
    int average( int first, ... );

    void main( void )
       /* Call with 3 integers (-1 is used as terminator). */
       printf( "Average is: 5Cn", average( 2, 3, 4, -1 ) );

       /* Call with 4 integers. */
       printf( "Average is: 5Cn", average( 5, 7, 9, 11, -1 ) );

       /* Call with just -1 terminator. */
       printf( "Average is: 5Cn", average( -1 ) );

    /* Returns the average of a variable list of integers. */
    int average( int first, ... )
       int count = 0, sum = 0, i = first;
       va_list marker;

       va_start( marker, first );     /* Initialize variable arguments. */
       while( i != -1 )
          sum += i;
          i = va_arg( marker, int);
       va_end( marker );              /* Reset variable arguments.      */
       return( sum ? (sum / count) : 0 );

Average is: 3
Average is: 8
Average is: 0

Just need to know more about va_list, va_start, va_arg a,d va_end.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.