Is the behaviour of * operator an example of operator oveloading in c?

K

karthik

The * operator behaves in 2 different ways. It is used as the value at
address operator as well as the multiplication operator. Does this mean
* is overloaded in c?
 
M

Michael Mair

karthik said:
The * operator behaves in 2 different ways. It is used as the value at
address operator as well as the multiplication operator. Does this mean
* is overloaded in c?

No, this is not overloading, it is multiple use of the same
character as operator.
One is as a unary operator, the other as a binary operator
and there is no danger that the compiler ever confuses them
in context[*].

If you want to see it as such, then overloading takes place
for the binary operator itself and the unary operator itself:
- binary * can multiplicate pairs of ints, unsigned ints, longs,
unsigned longs, doubles, long doubles (type promotion and
arithmetical conversions take care of the rest in C90), so
if "int" is your original argument type, then the rest is
overloaded.
- unary * can get the value of the different builtin types
_and_ for user defined types like structures, i.e. whenever
you write "struct foo {....};", * is overloaded, such that
for "struct foo *pFoo;" applying * to pFoo yields a
struct foo object.

Another point of view is to say that there is no operator
overloading in standard C.

At least one C implementation provides operator overloading
as compiler extension[**].

With C99, there are "type generic math functions" which
effectively are overloaded but there is no function
overloading as such.


Cheers
Michael

[*] This danger is given for "i---j"
[**] lcc-win32
 
K

karthik

Thanks 4 ur explanation Mr. Michael.
Michael said:
karthik said:
The * operator behaves in 2 different ways. It is used as the value at
address operator as well as the multiplication operator. Does this mean
* is overloaded in c?

No, this is not overloading, it is multiple use of the same
character as operator.
One is as a unary operator, the other as a binary operator
and there is no danger that the compiler ever confuses them
in context[*].

If you want to see it as such, then overloading takes place
for the binary operator itself and the unary operator itself:
- binary * can multiplicate pairs of ints, unsigned ints, longs,
unsigned longs, doubles, long doubles (type promotion and
arithmetical conversions take care of the rest in C90), so
if "int" is your original argument type, then the rest is
overloaded.
- unary * can get the value of the different builtin types
_and_ for user defined types like structures, i.e. whenever
you write "struct foo {....};", * is overloaded, such that
for "struct foo *pFoo;" applying * to pFoo yields a
struct foo object.

Another point of view is to say that there is no operator
overloading in standard C.

At least one C implementation provides operator overloading
as compiler extension[**].

With C99, there are "type generic math functions" which
effectively are overloaded but there is no function
overloading as such.


Cheers
Michael

[*] This danger is given for "i---j"
[**] lcc-win32
 

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,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top