[functions] - opcionals parameters
i know that in C\C++ we can create opcionals parameters, like the printf() function:
printf(char *string, ...); // i have seen these header in stdio.h
how can i do that?
i need build 1 function that accpet several string parameters, but i don't know work with them:(
any advices?
Re: [functions] - opcionals parameters
You do it just like that. It's not like optional parameters in VB or C# but more like a ParamArray, i.e. it specifies that the method will accept zero, one or more arguments. You can't specify the number and type of the parameters that way.
Re: [functions] - opcionals parameters
Quote:
Originally Posted by
jmcilhinney
You do it just like that. It's not like optional parameters in VB or C# but more like a ParamArray, i.e. it specifies that the method will accept zero, one or more arguments. You can't specify the number and type of the parameters that way.
i can do with va_list. but i don't understand how can i show the strings:(
Code:
#include <stdio.h>
#include <conio.h>
#include <stdarg.h>
int Menu( char *sentence, ... )
{
va_list listPointer;
va_start( listPointer, sentence );
for( int i = 0 ; i < numargs; i++ )
{
char *arg = va_arg(listPointer, char );
printf( "%s", i, arg );
}
va_end( listPointer );
return 0;
}
void main()
{
Menu("hello", "hi", "back", "Down");
getch();
}
the 'for' isn't correct but what i read was for 'int' and not 'char' - string's :(
Re: [functions] - opcionals parameters
This is a worthwhile read:
http://www.informit.com/guides/conte...lus&seqNum=138
It also mentions default arguments as an alternative, so that's basically optional parameters.
Re: [functions] - opcionals parameters
Quote:
Originally Posted by
jmcilhinney
thanks