468,289 Members | 1,766 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,289 developers. It's quick & easy.

Fooling C Compiler

Hi,

Is there a possibility to fool the C compiler into believing that two
data types are different by renaming one of them.
I tried using typedef. But it did not work out. :(:( . C compiler was
clever.
Is there any other way ?

Thx in advans,
Karthik Balaguru

Sep 3 '07 #1
4 1137
karthikbalaguru wrote:
Is there a possibility to fool the C compiler into believing that two
data types are different by renaming one of them.
I tried using typedef. But it did not work out. :(:( . C compiler was
clever.
Is there any other way ?
The better question is: why do you want to do this? Because answering
that will help us suggest alternatives.

--
Suspecting Abstract Types Hedgehog
Otherface: Jena RDF/Owl toolkit http://jena.sourceforge.net/

Sep 3 '07 #2
karthikbalaguru wrote:
Hi,

Is there a possibility to fool the C compiler into believing that two
data types are different by renaming one of them.
I tried using typedef. But it did not work out. :(:( . C compiler was
clever.
Is there any other way ?
Perhaps. Is there a possibility to fool your doctor
so your illness goes undetected and untreated? Is there
a possibility to fool the tax collector so he forgets you
have already paid and allows you to pay twice? Is there
a possibility to fool the Fire Department so their trucks
will go to a different address when your house burns?

"If you lie to the compiler, it will get its revenge."
-- Henry Spencer
--
Eric Sosman
es*****@ieee-dot-org.invalid
Sep 3 '07 #3
karthikbalaguru wrote:
Hi,

Is there a possibility to fool the C compiler into believing that two
data types are different by renaming one of them.
I tried using typedef. But it did not work out. :(:( . C compiler was
clever.
Is there any other way ?

Thx in advans,
Karthik Balaguru
Can you tell us what exactly you want to achieve ? Perhaps, struct or union
might be helpful.

Tejas Kokje
Sep 3 '07 #4
Eric Sosman <es*****@ieee-dot-org.invalidwrites:
karthikbalaguru wrote:
>Is there a possibility to fool the C compiler into believing that two
data types are different by renaming one of them.
I tried using typedef. But it did not work out. :(:( . C compiler was
clever.
Is there any other way ?

Perhaps. Is there a possibility to fool your doctor
so your illness goes undetected and untreated? Is there
a possibility to fool the tax collector so he forgets you
have already paid and allows you to pay twice? Is there
a possibility to fool the Fire Department so their trucks
will go to a different address when your house burns?
That's a bit unfair. I assume "fooling the compiler" was meant to be
facetious.

It reasonable, given the name, to assume that a "typedef" defines a
new and distinct type, rather than just an alias for an existing type.
It's also reasonable to want to create a new and distinct type, so
that the compiler will warn you about any attempt to mix the new type
with the type on which it's based. I think that's what the OP is
trying to do, but he's just discovered that 'typedef' isn't the way to
do it.

Some languages let you do this easily. For example, Ada's
type My_Integer is new Integer;
creates a new type called My_Integer. It has the same characteristics
as Integer, but, for example, attempting to assign an Integer value to
a My_Integer object, or vice versa, is illegal.

C doesn't have such a facility, at least not directly. But like
anything else, you can do it indirectly.

The simplest way to create a new and disinct type is to wrap an
existing type in a structure:

struct my_int {
int i;
};
int x;
my_int y;
/* ... */
x = y; /* ILLEGAL */
x = y.i; /* ok */

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 3 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Bryan Parkoff | last post: by
10 posts views Thread by Bjorn | last post: by
14 posts views Thread by joshc | last post: by
reply views Thread by rollasoc | last post: by
3 posts views Thread by Mark Rockman | last post: by
41 posts views Thread by Miroslaw Makowiecki | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.