Functions returning arbitrary type

A

Andrej Prsa

Hello, List!

Does ANSI C support in any way functions that return different types
depending on the given arguments? I.e.

int my_function (int arg)
double my_function (double arg)
char *my_function (char *arg)

to be joined in a single my_function somehow? I tried playing with void*,
but I didn't get far really.

Help?

Thanks,

Andrej
 
K

Kevin Easton

Andrej Prsa said:
Hello, List!

Does ANSI C support in any way functions that return different types
depending on the given arguments? I.e.

int my_function (int arg)
double my_function (double arg)
char *my_function (char *arg)

to be joined in a single my_function somehow? I tried playing with void*,
but I didn't get far really.

Help?

No. The closest you can get is using a union:

union foo {
int i;
double d;
char *cp;
};

....but you'll still need to tell your function what kind of type you
actually passed it in the union, so the function will need code
equivalent to:

switch (type_used) {
case TYPE_INT:
/* retrieve int argument */
break;
case TYPE_DOUBLE:
/* retrieve double argument */
break;
case TYPE_CHAR_P:
/* retrieve char * argument */
break;
default:
}

/* Common code */

switch (type_used) {
case TYPE_INT:
/* return int argument */
/* ... */

So you might as well just have a function like:

int my_func_int(int n)
{
/* convert int to type common code expects */
y = my_func_common(x);
/* convert y to int and return it */
}

...and similar for the double and char * cases - this lets you factor out
the common case.

- Kevin.
 
J

Joe Wright

Andrej said:
Hello, List!

Does ANSI C support in any way functions that return different types
depending on the given arguments? I.e.

int my_function (int arg)
double my_function (double arg)
char *my_function (char *arg)

to be joined in a single my_function somehow? I tried playing with void*,
but I didn't get far really.

Help?
We are not a List, thank you. Your question describes one of the many
ways C++ differs from C, function overloading. I think you must already
know this. Any book on C++ explains it and no book on C mentions it.

The short answer is, of course, no.
 
M

Malcolm

Joe Wright said:
We are not a List, thank you. Your question describes one of the many
ways C++ differs from C, function overloading. I think you must already
know this. Any book on C++ explains it and no book on C mentions it.
If the OP learned C++ before C, why would he know that this feature is not
available in C?
 
J

Joe Wright

Malcolm said:
If the OP learned C++ before C, why would he know that this feature is not
available in C?

It's my sense of smell. My own minor C++ book explains overloading,
mangling, etc. in C++ and explains that C can't do it. A C++ weenie
would know that much. I smell troll.

But hey, what do I know? There's pollen, dander..
 
D

Dave Thompson

Hello, List!

Does ANSI C support in any way functions that return different types
depending on the given arguments? I.e.

int my_function (int arg)
double my_function (double arg)
char *my_function (char *arg)
C99 extends several of the standard-library math functions in
<tgmath.h> to be overloaded on the three standard floating types.
There is (and was) no requirement for a similar facility for user
functions, although some implementations may do so anyway.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top