473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How do I overload functions in C?

I know that C++ lets you overload functions, but how do I overload functions in C?
Nov 13 '05 #1
13 44173
some one <fr********@yahoo.com> scribbled the following:
I know that C++ lets you overload functions, but how do I overload functions in C?


You don't.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
Nov 13 '05 #2
some one wrote:
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.

--
Martin Ambuhl

Nov 13 '05 #3
In article <54*************************@posting.google.com> , some one wrote:
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.

--
Andreas Kähäri
Nov 13 '05 #4
You can't.
But perhaps you want to check the "..." that allows you to set
unlimited/variable argument list.

--
Elias
http://lgwm.org/
"some one" <fr********@yahoo.com> wrote in message
news:54*************************@posting.google.co m...
I know that C++ lets you overload functions, but how do I overload

functions in C?
Nov 13 '05 #5
> > I know that C++ lets you overload functions, but how do I overload
functions in C?
You don't.

More precisely, you CAN'T.
Nov 13 '05 #6
fr********@yahoo.com (some one) wrote in message news:<54*************************@posting.google.c om>...
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;
}
Nov 13 '05 #7
some one wrote:
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.

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

"some one" <fr********@yahoo.com> wrote in message
news:54*************************@posting.google.co m...
I know that C++ lets you overload functions, but how do I overload functions in C?

Nov 13 '05 #9

On Mon, 13 Oct 2003, jacob navia wrote:

"some one" <fr********@yahoo.com> wrote [...]
I know that C++ lets you overload functions, but how do
I overload functions in C?


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

Nov 13 '05 #10
On 2003-10-13, some one <fr********@yahoo.com> wrote:
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
Nov 13 '05 #11
jacob navia wrote:
With lcc-win32 you write:

[stuff that isn't C and isn't topical in comp.lang.c]

--
Martin Ambuhl

Nov 13 '05 #12

"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message
news:Pi***********************************@unix46. andrew.cmu.edu...

On Mon, 13 Oct 2003, jacob navia wrote:

"some one" <fr********@yahoo.com> wrote [...]
I know that C++ lets you overload functions, but how do
I overload functions in C?


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

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?
Nov 13 '05 #13
"jacob navia" <ja*********@jacob.remcomp.fr> wrote in message news:<bm**********@news-reader4.wanadoo.fr>...
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message
news:Pi***********************************@unix46. andrew.cmu.edu...

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...


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.
Nov 13 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: David Sachs | last post by:
The following program illustrates an interesting effect of the way C++ resolves function overloading. I have verified with a member of the C++ stardard committee that the output shown is...
7
by: Piotre Ugrumov | last post by:
I have tried to implement the overload of these 2 operators. ostream & operator<<(ostream &out, Person &p){ out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl; return out; }...
1
by: Piotre Ugrumov | last post by:
I'm following your help. I have written the overload of the operator <<. This overload work! :-) But I have some problem with the overload of the operator >>. I have written the overload of this...
4
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement...
4
by: Arturo Cuebas | last post by:
The program below contains a compile error. Following the program you will find the typical fix then my idea for a library that facilitates a more elegant fix. #include <boost\bind.hpp> using...
18
by: skishorev | last post by:
Hi, Here I am taking two functions. void f(int,int) and another one is float f(int,int). Is it possible to overload with return values. Thx, kishore
2
by: xtrigger303 | last post by:
Hi to all, I was reading Mr. Alexandrescu's mojo article and I've a hard time understanding the following. Let's suppose I have: //code struct A {}; struct B : A {};
9
by: sebastian | last post by:
I've simplified the situation quite a bit, but essentially I have something like this: struct foo { }; void bar( foo const & lhs, foo const & rhs )
5
by: jknupp | last post by:
In the following program, if the call to bar does not specify the type as <int>, gcc gives the error "no matching function for call to ‘bar(A&, <unresolved overloaded function type>)’". Since bar...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.