I need to develop a function in C that will be called with the same definition with or without parameters like this:
char *fnc(???)
{
char *str;
....
return str;
}
char *c1 = fnc();
char *c2 = fnc(param);
How to do it?
I need to develop a function in C that will be called with the same definition with or without parameters like this:
char *fnc(???)
{
char *str;
....
return str;
}
char *c1 = fnc();
char *c2 = fnc(param);
How to do it?
This is called Function Overloading You can't do it in C
You can't achieve exactly what you want in C.
You do have some options available:
declare your function as a varargs function and use the va_ macros to access the optional parameters (note that in this case you need to have at least one fixed - non-optional - argument, and you also need to know the types of the remaining arguments you are passed)
use a variadic macro definition to wrap the function name and call the real function as needed - you'd still need to make the real function variadic, but variadic macros don't require a fixed argument first.
If either of these sound helpful I can elucidate with examples.
It is possible using the old varargs.h
There you have
void varfunc( va_alist )
va_dcl
{
va_list args;
char *p1;
va_start( args );
p1 = va_arg( args, char * ); /* any type is possible */
...
va_end( args );
}
Using the modern stdarg.h
, it's not possible anymore. You can define function with variable arguments, but you must always have one fixed as it is:
void varfunc( int fixedArg, ... ) // of course fixedArg can be of any type
{
va_list args;
char *p1;
va_start( args, fixedArg ); // here you need the fixed one
p1 = va_arg( args, char * ); // again any type i possible
...
va_end( args );
}
There is a quite obvious reason why a syntax with no fixed arguments doesn't make sense: as there is no portable way to determine how many and what type of arguments have been passed, you need that information in a fixed argument, which is e.g. the format string in the printf()
family or maybe just a number of arguments, some bit flags or whatever.
Use C++
rather than C
, or pass a NULL
pointer:
void fnc(char* Optional)
{
if (Optional != NULL)
{
// do something amazing
}
}
fnc(NULL); // "Not passing" a parameter
fnc("hello");
In C you can't do that directly, you'd have to go through a macro. Variable argument macros are different from variable argument functions because they allow the argument to be empty. Then you have to detect if the argument is in fact empty and to provide a default argument to the final function call.
I have a macro package P99 that does all that wrapping for you. One possibility would be to declare something like
#define fnc(...) P99_CALL_DEFARG(fnc_imp, 1, __VA_ARGS__)
#define fnc_defarg_0() (char const*)0
void fnc_imp(char const* arg) {
if (!arg) {
/* do something special */
}
/* your code */
}
Note that in C a macro may have the same name as a function, if you don't find that confusing, so you could even name the function itself fnc
, too.
The correct way to solve the problem is to recognise that you shouldn't be using one function for 2 different cases in the first place; as this is mostly pointless and creates code maintenance problems (e.g. makes it hard to detect "wrong number of arguments" bugs).
For example, you could have a foo_simple(void)
that (internally) calls foo_complex(arg1, arg2)
; where other code can use whichever function makes them happy.
There seems to be a lot of misguided answers here. What you want is like printf, which can take 0 arguments, or it can take many. To mimick this, you use the ... specifier in the function.
function variableArguments(void * typeDefinition, ...);
you can cast the void * to whatever you want, even if its not a pointer by passing in the address of a non pointer value.
You could use a combination of things:
#define function() function(NULL)
void function(void * parameter)
{
if (parameter != NULL)
{ /*code*/ }
}
void function(void * parameter)
{
if (parameter != NULL)
{ /*code*/ }
}
call: func(NULL); or func(ptr);
use a va_list
i'm unsure if C handles this or not, but you /may/ be able to use default parameters
void function(void * parameter = NULL)
{
if (parameter != NULL)
{ /*code*/ }
}
In C++, this is easy:
char *fnc(parameter=default_value){
....
}
So, if you call to the function without parameters, the value of the function parameter will be the "default_value". If you call to the function with a parameter, the value of the function parameter will be the one in the function call. If you have more than 1 parameter, don't forget to put the parameter with a default value at the end of the parameter list!
BTW, sorry for my bad english and if I'm wrong, tell me, please. I'm a programming aprentice :-)