|
-
Mar 21st, 2013, 03:28 PM
#1
Thread Starter
PowerPoster
[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?
-
Mar 21st, 2013, 06:56 PM
#2
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.
-
Mar 22nd, 2013, 05:25 AM
#3
Thread Starter
PowerPoster
Re: [functions] - opcionals parameters
 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
-
Mar 22nd, 2013, 05:38 AM
#4
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.
-
Mar 22nd, 2013, 05:46 AM
#5
Thread Starter
PowerPoster
Re: [functions] - opcionals parameters
 Originally Posted by jmcilhinney
thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|