How to undef a typedef??

I

informativeguy

Hi,
I did
typedef int Man;

After some code I get a necessity to use the same name Man for char. I
mean to say,I need to
typedef char Man;
But how can I do this???
 
C

Chris Dollin

Hi,
I did
typedef int Man;

After some code I get a necessity to use the same name Man for char. I
mean to say,I need to
typedef char Man;
But how can I do this???

To a good first approximation [1], you can't.

Perhaps you could explain why you've painted yourself into this
corner? We can then discuss the alternatives to paint.

[1] No nested scopes.
 
E

Eric Sosman

Hi,
I did
typedef int Man;

After some code I get a necessity to use the same name Man for char. I
mean to say,I need to
typedef char Man;
But how can I do this???

Suggestion #1: Don't.

Suggestion #2: Put the two different `Man' uses in different
scopes. For example, use one typedef in function f() and the
other in function g(), and neither at file scope. Or use one at
file scope in source file a.c and the other in source file b.c,
compiled separately.
 
T

Tor Rustad

Hi,
I did
typedef int Man;

After some code I get a necessity to use the same name Man for char. I
mean to say,I need to
typedef char Man;
But how can I do this???

You can't use

#undef <identifier>

because the specified <identifier> has to be a macro name, else the
undef will be ignored. However, this works:

#include <stdio.h>
#include <limits.h>

void typedef_func1(void)
{
typedef int Man;
Man i;

i = INT_MAX;

printf("INT_MAX: %d\n", i);
}

void typedef_func2(void)
{
typedef char Man;
Man c;

c = CHAR_MAX;

printf("CHAR_MAX: %d\n", (int)c);
}

int main(void)
{
typedef_func1();
typedef_func2();

return 0;
}
 
K

Kenneth Brody

Hi,
I did
typedef int Man;

After some code I get a necessity to use the same name Man for char. I
mean to say,I need to
typedef char Man;
But how can I do this???

As others have already said, "you can't" and "why would you want to?"

However, you can get close with something like:

typedef whatever Man_t1;
typedef something_else Man_t2;

#define Man Man_t1
...
#undef Man
#define Man Man_t2

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
F

Flash Gordon

osmium said:
"Kenneth Brody" writes:

Please don't snip the attributions for things you are still quoting.
Kenneth definitely did *not* write the original question. I've
reinstated the attribution for the OPs question.
Perhaps there is more than one programmer involved?

That would be an excellent reason to NOT do it. As others have
suggested, what the OP is trying to do is a very bad idea.
 
O

osmium

Please don't snip the attributions for things you are still quoting.
Kenneth definitely did *not* write the original question. I've reinstated
the attribution for the OPs question.


That would be an excellent reason to NOT do it. As others have suggested,
what the OP is trying to do is a very bad idea.

Sorry, I thought must of the people on here could count angle brackets and
figure out who is being responded to.

Hint: One angle bracket is smaller than two angle brackets, therefore the
response was directed at Kenneth Brody. I am not the least bit interested
as to who *he* responded to. I guess I read "why would you want to" as
"why would you need to" or "how did you get in this predicament". I agree
it is a bad idea and tried not to comment on the advisability of doing that.
The OP had already been told several times not to do it and that he can't.
I tried to respond to what I saw as the "new" thing in the thread.
 
J

John Bode

How on Earth did you wind up in that situation?
Suggestion #1: Don't.

Suggestion #2: Put the two different `Man' uses in different
scopes. For example, use one typedef in function f() and the
other in function g(), and neither at file scope. Or use one at
file scope in source file a.c and the other in source file b.c,
compiled separately.

Which is still going to be a disaster in its own way. Using the same
name to refer to different types is going to cause problems somewhere
down the line.

It may be time to take a step back and rethink the program's design.
 
R

Random832

2006-11-16 said:
Hint: One angle bracket is smaller than two angle brackets, therefore the
response was directed at Kenneth Brody. I am not the least bit interested
as to who *he* responded to.

You're still supposed to have exactly as many attribution lines as you
have quote levels. Not more, not less.
 
P

Peter Nilsson

John said:
How on Earth did you wind up in that situation?


Which is still going to be a disaster in its own way. Using the same
name to refer to different types is going to cause problems somewhere
down the line.

Yes, but possibly because of C's limitations.
It may be time to take a step back and rethink the program's design.

Or step over to C++. [cf size_t (et al) in the STL.]
 
W

whua113

"Tor Rustad дµÀ£º
"
You can't use

#undef <identifier>

because the specified <identifier> has to be a macro name, else the
undef will be ignored. However, this works:

#include <stdio.h>
#include <limits.h>

void typedef_func1(void)
{
typedef int Man;
Man i;

i = INT_MAX;

printf("INT_MAX: %d\n", i);
}

void typedef_func2(void)
{
typedef char Man;
Man c;

c = CHAR_MAX;

printf("CHAR_MAX: %d\n", (int)c);
}

int main(void)
{
typedef_func1();
typedef_func2();

return 0;
}


#include <stdio.h>
#include <limits.h>

typedef int Man;

void typedef_func1(void)
{
Man i;
i = INT_MAX;
printf("INT_MAX: %d\n", i);
}

#ifdef Man
#undef Man
typedef char Man;
#endif

void typedef_func2(void)
{
Man c;
c = CHAR_MAX;
printf("CHAR_MAX: %d\n", (int)c);
}


int main(void)
{
typedef_func1();
typedef_func2();

return 0;
}
 
J

Jack Klein

"Tor Rustad дµÀ£º
"


#include <stdio.h>
#include <limits.h>

typedef int Man;

void typedef_func1(void)
{
Man i;
i = INT_MAX;
printf("INT_MAX: %d\n", i);
}

#ifdef Man
#undef Man
typedef char Man;

Did you compile this? What diagnostic did your compiler emit when it
processed the line above.
#endif

void typedef_func2(void)
{
Man c;
c = CHAR_MAX;
printf("CHAR_MAX: %d\n", (int)c);
}


int main(void)
{
typedef_func1();
typedef_func2();

return 0;
}

Absolutely, positively, for sure you did not compile this with a C
compiler.

Either you did not compile it at all, or you compiled it with
something that does not claim to be a C compiler.
 
S

santosh

"Tor Rustad дµÀ£º
"
#include <stdio.h>
#include <limits.h>

typedef int Man;

void typedef_func1(void)
{
Man i;
i = INT_MAX;
printf("INT_MAX: %d\n", i);
}

#ifdef Man
#undef Man
typedef char Man;
#endif

This shouldn't work on a conforming C compiler. Preprocessing is done
before compilation proper, thus in the above code, Man should always be
undefined.
 
K

Kenneth Brody

Jack said:
On 16 Nov 2006 18:41:39 -0800, "(e-mail address removed)" <[email protected]>
wrote in comp.lang.c: [...]
#include <stdio.h>
#include <limits.h>

typedef int Man;

void typedef_func1(void)
{
Man i;
i = INT_MAX;
printf("INT_MAX: %d\n", i);
}

#ifdef Man
#undef Man
typedef char Man;

Did you compile this? What diagnostic did your compiler emit when it
processed the line above.

Actually, no diagnostic is required, since "#ifdef Man" is false.
Absolutely, positively, for sure you did not compile this with a C
compiler.

Either you did not compile it at all, or you compiled it with
something that does not claim to be a C compiler.

It should "work" just fine. However, what it does is not what
whua113 thinks it does. Because the "#ifdef Man" is false, the
supposed redefinition of Man never takes place. Man is still a
typedef for int. Since an int can contain CHAR_MAX, the output
"looks right".

Change the printfs to:

printf("INT_MAX: %d, sizeof(Man) is %d\n", (int)c,(int)sizeof(Man));
and
printf("CHAR_MAX: %d, sizeof(Man) is %d\n", (int)c,(int)sizeof(Man));

and you will see that sizeof(Man) is the same in both. (On my
system, they are both 4. Since sizeof char is defined as "1",
you know that Man cannot be a typedef of char.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Kenneth Brody

santosh said:
(e-mail address removed) wrote: [...]
#ifdef Man
#undef Man
typedef char Man;
#endif

This shouldn't work on a conforming C compiler. Preprocessing is done
before compilation proper, thus in the above code, Man should always be
undefined.
[...]

I guess it depends on your definition of "works". It "works" in the
sense that it is a valid, conforming C program. It doesn't "work"
in the sense that the typedef is not done, as the ifdef is false.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top