views:

216

answers:

5

Converting a C++ lib to ANSI C and it seems like though ANSI C doesn't support default values for function variables or am I mistaken? What I want is something like

int funcName(int foo, bar* = NULL);

Also, is function overloading possible in ANSI C?

Would need

const char* foo_property(foo_t* /* this */, int /* property_number*/);

const char* foo_property(foo_t* /* this */, const char* /* key */, int /* iter */);

Could of course just name them differently but being used to C++ I kinda used to function overloading.

+9  A: 

No, Standard C does not support either. Why do you feel you need to convert your C++ code to C? That could get quite tricky - I'd have thought writing wrappers would be the way to go, if your C++ must be callable from C.

anon
I'm now working in a C environment and the use of C++ code has been banned ;)... So instead of making the existing C compliant with the g++ compiler and thus being able to use C++ classes alongside the C code I'm forced to convert existing useful classes I have to C.
inquam
@ inquam: Don't. Use the language at hand, don't try to force some other language down the throat of the compiler. While it is comparatively easy to make C code work in a C++ environment, the opposite is anything but trivial (as you just came to realize). Rather re-implement your "useful classes" as a C function library than trying to "port" it 1:1.
DevSolar
@inquam In general, I agree with DevSolar. Particularly if you have used any C++ features such as Standard Library containers, exceptions and particularly RAII. However, if your original code simply ised C++ as "a better C", then I guess it is just about doable, though I wouldn't want to be the one doing it.
anon
This specific library was written using c-style strings and arrays so no STL was involved. So making it work (which I now have) was basicly about removing the class structure and making functions passing the initially created object to each one. One "new" function to create the object and then use that as one of the arguments for all the other functions.
inquam
A: 

Neither of default values or function overloading exists in ANSI C, so you'll have to solve it in a different way.

Hans Insulander
+2  A: 

As far as I know ANSI C doesn't directly support function overloading or default arguments. The standard substitute for overloading is adding suffixes to the function name indicating the argument types. For example, in OpenGL, a "3fv" suffix to a function name means the function takes a vector of three floats.

Default arguments can be viewed as a special case of function overloading.

Amnon
+1  A: 

You can't so easily since C does not support them. The simpler way to get "fake overloading" is using suffixes as already said... default values could be simulated using variable arguments function, specifying the number of args passed in, and programmatically giving default to missing one, e.g.:

 aType aFunction(int nargs, ...)
 {
   // "initialization" code and vars
   switch(nargs)
   {
     case 0:
         // all to default values... e.g.
         aVar1 = 5; // ...
         break;
     case 1:
         aVar1 = va_arg(arglist, int); //...
         // initialize aVar2, 3, ... to defaults...
         break;
     // ...
   }
 }

Also overloading can be simulated using var args with extra informations to be added and passed and extracode... basically reproducing a minimalist object oriented runtime ... Another solution (or indeed the same but with different approach) could be using tags: each argument is a pair argument type + argument (an union on the whole set of possible argument type), there's a special terminator tag (no need to specify how many args you're passing), and of course you always need "collaboration" from the function you're calling, i.e. it must contain extra code to parse the tags and choose the actual function to be done (it behaves like a sort of dispatcher)

ShinTakezou
A: 

You'll have to declare each C++ overloaded function differently in C because C doesn't do name mangling. In your case "foo_property1" "foo_property2".

Iulian Şerbănoiu