•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 455,985 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,772 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 2466 | Replies: 1
![]() |
•
•
Join Date: Aug 2004
Posts: 138
Reputation:
Rep Power: 5
Solved Threads: 1
How do I create a function with variable parameters. Will "functionname(float a, ...);" work? If it does how do I use the variables.
Eg.
:cry: :lol: :rolleyes:
Eg.
void f1(int i, ...);
void main()
{
int a, b,c;
cout<<"Enter 3 values";
cin>>a>>b>>c;
f1(a, b, c);
}
void fi()
{
//How do I access the variables that are passed as parameters
}:cry: :lol: :rolleyes:
•
•
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation:
Rep Power: 5
Solved Threads: 10
You need at least one real parameter, as you have in your example. Then you use some special macros to access the parameters:
The caviats are:
1) You do not know how many parameters were supplied. Thats why in my sample I had that be the one 'real' parameter.
2) You do not know the type of parameters, so you have to 'guess' and hope the caller knows what they are doing!
printf() and its variants use this, and the format string specifies the number and type of parameters:
printf( "%s %d %x\n", "string", number, number );
but if you screw up you get fun results!
printf( "%s %d %d\n", number, "string", 1.2 ); // ick!
void AddSomeNumbers( int howMany, .... ) // any number of 'real' params, folowed by ...
{
va_list argptr;
int ret = 0;
va_start( argptr, howMany ); // use the last 'real' param here
for (int i = 0; i < howMany; i++)
ret += va_arg( argptr, int ); // the next arg is an int, we hope
va_end(argptr);
return ret;
}The caviats are:
1) You do not know how many parameters were supplied. Thats why in my sample I had that be the one 'real' parameter.
2) You do not know the type of parameters, so you have to 'guess' and hope the caller knows what they are doing!
printf() and its variants use this, and the format string specifies the number and type of parameters:
printf( "%s %d %x\n", "string", number, number );
but if you screw up you get fun results!
printf( "%s %d %d\n", number, "string", 1.2 ); // ick!
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Can't determine winner for Tic Tac Toe (C++)
- help on passing variable to a function? (JavaScript / DHTML / AJAX)
- Program works... now I need to create a function, HELP! (C)
- Terminating a String (C)
- function passing variables (C)
- error checking of user input (C++)
- having a small problem with the gets function (C)
Other Threads in the C Forum
- Previous Thread: reading and printing a file to screen in C
- Next Thread: Help in Input output C files


Linear Mode