Re: c question



> 
> In know in other languages you can specify a default value for a variable
> in a function decleration. Can you do something like this in c?
> 
> void function(char *string="default");
> 
> and be able to call it with
> 
> function();
> 
> ?

Not really, you have to use varargs functions, if you want non-specified
arguments, and then you need to process them in some manner and know
when they run out, e.g. by ending the list with a null.

Although its better probably not to use varags unless you really
dont know how many arguments you're gonna get (like printf() for
example).

If you really want to do this, use some trivial helper functions
which call a 'meta' function which takes all possible arguments.
Each helper function has the desired signature, and then it
just calls the main function with the required defaults for
arguments it doesn't accept (there are examples of this
throughout gnome).

You could probably also use macro's to do something similar
as well, but their suitability depends on the situtation.

 Michael




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]