Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 1998-06-06
Medium Priority
Last Modified: 2010-04-15
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.

Question by:Marc_P

Accepted Solution

Slarti earned 210 total points
ID: 1251137
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.

Expert Comment

ID: 1251138
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
LVL 85

Expert Comment

ID: 1251139
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)

LVL 11

Expert Comment

ID: 1251140
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


Expert Comment

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


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question