Hello
I have to function

void lala_a(adktis *p)
void lala_k(kdktis *p)

These two functions are doing the same things but i want to make one function tha can accept adktis/kdktis something like this

void lala(adktis *p);
void lala(kdktis *p);

Is there a way to do this in c?
Thank you very much

Recommended Answers

All 5 Replies

Is there a way to do this in c?

In standard C, the short answer is no. The long answer is that you can do it with a lot of fuss and bother, but ultimately it isn't worth the effort for a weak facsimile of function overloading. The alternative answer is if you're willing to sacrifice the full behavior of overloading (ie. the correct function body is magically chosen), you can make a variant type using unions or use variable argument lists to hack something reasonably close to the end result. Once again, it's overly complicated for the minor convenience you get.

If you really want this behavior and your compiler supports overloading as an extension, that's the direction you should go.

Also, the next C standard has a feature in the works that would theoretically accomplish what you want:

#define lala(x) _Generic((x), adktis*: lala_a, kdktis*: lala_k)(x)

Not that it helps you now, of course. ;)

commented: For the psychic poster +13

but ultimately it isn't worth the effort for a weak facsimile of function overloading.

Wow you got function overloading from the original posting? You must be psychic.

thank you for your answer. Should be good to do void lala(void * dktis)? Or this too generic to do it?
Thank you

Should be good to do void lala(void * dktis)? Or this too generic to do it?

You still need a way to differentiate between adktis and kdktis.

So the best solution is that i did or the other you suggested? Is a way to make a new object that has type adktis or kdktis and put it as input in the function?
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.