How do I overload functions in C?

S

some one

I know that C++ lets you overload functions, but how do I overload functions in C?
 
M

Martin Ambuhl

some said:
I know that C++ lets you overload functions, but how do I overload functions in C?

You don't.
Whether overloading functions is a good thing is not decidable. The C++
designers have a different view from that of C designers.
 
A

Andreas Kahari

I know that C++ lets you overload functions, but how do I
overload functions in C?


You can't.

There is one type of overloaded functions in C, the generic math
functions, but there's no way to overload functions yourself as
you may do in e.g. C++ or other languages.
 
E

E. Robert Tisdale

some said:
I know that C++ lets you overload functions, but how do I overload functions in C?
Manually.

> cat complex.c
#include<complex.h>
#include<stdio.h>

int main(int argc, char* argv[]) {
double complex z = I;
double x = cabs(z);
fprintf(stdout, "%f = cabs(z)\n", x);
return 0;
}

You must manually *mangle* the function name --
usually by *decorating* it with prefixes and/or suffixes --
to create a unique *symbol* for each function argument type
that the link editor can use to find the appropriate function.
In the above example, 'c' is prepended to the abs function name
to calculate the absolute value of a complex number.
C++ compilers simply mangle the function name automatically
for C++ programmers depending upon the argument types.
In general, different C++ compilers use different *mangling schemes*
so it usually difficult to link object files
compiled by different C++ compilers.
 
J

John Bode

I know that C++ lets you overload functions, but how do I overload functions in C?

You can't, at least not in the C++ sense. You can fake it, sort of,
by using variable-length argument lists, but this isn't truly
overloading a function name.

Here's one approach. We create three functions to deal with different
type arguments (one handles double, one int, one char*). They're
called from one function with a variable-length argument list. The
first argument is mandatory, and it tells us what type of argument to
expect.

The va_start function sets ap to point to the first argument following
our fixed argument. The va_arg function returns the value of the
argument pointed to by ap, based on the type provided, and advances
the ap pointer to the next argument in the list.

#include <stdarg.h>
#include <stdio.h>

void func_1 (double f) { printf ("f = %f\n", f); }
void func_2 (int i) { printf ("i = %d\n", i); }
void func_3 (char *s) { printf ("s = %s\n", s); }

void func (int usage, ...)
{
va_list ap;
va_start (ap, usage);

switch (usage)
{
case 1: func_1 (va_arg (ap, double)); break;
case 2: func_2 (va_arg (ap, int)); break;
case 3: func_3 (va_arg (ap, char *)); break;
default: printf ("huh?\n"); break;
}
}

int main (void)
{
func (1, 1.0);
func (2, 2);
func (3, "three");
func (4, 40);
return 0;
}
 
A

Arthur J. O'Dwyer

With lcc-win32 you write:
int overloaded fn(struct a*arg);
int overloaded fn(struct b *arg);

and when you pass to fn an "a" it will call the first, and when you
pass it the second it will call the second.

http://www.cs.virginia.edu/~lcc-win32

That's very interesting, Jacob, but the OP did ask about
the C language, not about lcc-win32.

Oh, and *please* don't top-post.

-Arthur
 
J

James Hu

I know that C++ lets you overload functions, but how do I overload
functions in C?

You cannot do so arbitrarily, all C solutions require some form of
trickery. Several have been offered to you.

One trickery not yet suggested is to employ a polymorphic interface.

typedef struct polymorph polymorph;
struct polymorph {
void (* const foo)(polymorph *);
};

void foo(polymorph *p) { p->foo(p); }


Now, you get a form of function overloading when you create instances
of polymorph.

struct A {
polymorph interface;
int a;
};

static void A_foo(polymorph *p) {
struct A *me = (void *)p;
printf("a:%d\n", a++);
}

static const polymorph A_interface = { A_foo };

polymorph * create_A(void) {
struct A *i = malloc(sizeof(struct A));
i->interface = A_interface;
i->a = 0;
}

struct B {
polymorph interface;
int b;
};

static void B_foo(polymorph *p) {
struct B *me = (void *)p;
printf("b:%d\n", b--);
}

static const polymorph B_interface = { B_foo };

polymorph * create_B(void) {
struct B *i = malloc(sizeof(struct B));
i->interface = B_interface;
i->b = 0;
}

Now, you can get a form of overloading with the foo() function.

polymorph *a = create_A(); /* a points to a struct A */
polymorph *b = create_B(); /* b points to a struct B */

foo(a); /* will output "a:0" */
foo(b); /* will output "b:0" */
foo(a); /* will output "a:1" */
foo(b); /* will output "b:-1" */

-- James
 
J

jacob navia

Arthur J. O'Dwyer said:
That's very interesting, Jacob, but the OP did ask about
the C language, not about lcc-win32.

Oh, and *please* don't top-post.

-Arthur
OK OK True, this is an lcc-win32 evil extension...
Sorry, I couldn't resist :)
If this question arises it is because people need that isn't it?
 
A

August Derleth

jacob navia said:
OK OK True, this is an lcc-win32 evil extension...

Not evil, just off-topic. It isn't polite to post things outside of
the topic in a newsgroup, especially a technical one.
If this question arises it is because people need that isn't it?

I've never needed it, and if I did I'd probably change languages.
(Although the idea of a variadic with the first argument declaring
type is neat...) But needs aren't really here or there when it comes
to topicality on this newsgroup. The topic is Standard C and/or
pre-Standard K&R C, and neither languages has any concept of function
overloading or polymorphism or inheritance or anything else of the
sort.

And, good job with curing yourself of top-posting! It's appreciated,
and makes people more likely to regard you as clueful.
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top