By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,510 Members | 1,839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,510 IT Pros & Developers. It's quick & easy.

Functions returning arbitrary type

P: n/a
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

Nov 13 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Andrej Prsa <an*********@guest.arnes.si> wrote:
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.

Nov 13 '05 #2

P: n/a
Andrej Prsa wrote:

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.
--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #3

P: n/a

"Joe Wright" <jo********@earthlink.net> wrote in message

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?
Nov 13 '05 #4

P: n/a
Malcolm wrote:

"Joe Wright" <jo********@earthlink.net> wrote in message

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?


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..
--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #5

P: n/a
On Sun, 6 Jul 2003 01:07:17 +0200, Andrej Prsa
<an*********@guest.arnes.si> wrote:
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
Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.