Fooling C Compiler

  • Thread starter karthikbalaguru
  • Start date
K

karthikbalaguru

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
 
B

Ben Pfaff

karthikbalaguru said:
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 ?

A typedef is really just an alias. As you found out, it doesn't
actually change the type's identity: the type and its alias are
treated identically.

The usual way to make two otherwise equivalent types appear
different is to encapsulate one of them in a struct.
 
C

Chris Dollin

karthikbalaguru said:
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.
 
E

Eric Sosman

karthikbalaguru said:
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
 
T

Tejas Kokje

karthikbalaguru said:
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
 
K

Keith Thompson

Eric Sosman said:
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 */
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top