By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,017 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,017 IT Pros & Developers. It's quick & easy.

How to undef a typedef??

P: n/a
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???

Nov 16 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
in************@gmail.com wrote:
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.

--
Chris "hantwig efferko VOOM!" Dollin
"Never ask that question!" Ambassador Kosh, /Babylon 5/

Nov 16 '06 #2

P: n/a
in************@gmail.com wrote:
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.

--
Eric Sosman
esosman@acm
Nov 16 '06 #3

P: n/a
in************@gmail.com wrote:
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 <identifierhas 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;
}

--
Tor <torust AT online DOT no>

Nov 16 '06 #4

P: n/a
in************@gmail.com wrote:
>
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:Th*************@gmail.com>

Nov 16 '06 #5

P: n/a
"Kenneth Brody" writes:
>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?"
Perhaps there is more than one programmer involved?
Nov 16 '06 #6

P: n/a
osmium wrote:
"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.
>>in************@gmail.com wrote:
>>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?"

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.
--
Flash Gordon
Nov 16 '06 #7

P: n/a
"Flash Gordon" <sp**@flash-gordon.me.ukwrote in message
news:h1************@news.flash-gordon.me.uk...
osmium wrote:
>"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.
>in************@gmail.com wrote:
>>>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?"

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.
--
Flash Gordon
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.
Nov 16 '06 #8

P: n/a

Eric Sosman wrote:
in************@gmail.com wrote:
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;
How on Earth did you wind up in that situation?
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.
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.

Nov 16 '06 #9

P: n/a
2006-11-16 <4s************@mid.individual.net>,
osmium wrote:
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.
Nov 16 '06 #10

P: n/a
John Bode wrote:
Eric Sosman wrote:
in************@gmail.com wrote:
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;

How on Earth did you wind up in that situation?
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.

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

--
Peter

Nov 16 '06 #11

P: n/a

"Tor Rustad д
"
in************@gmail.com wrote:
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 <identifierhas 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;
}

--
Tor <torust AT online DOT no>

#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;
}

Nov 17 '06 #12

P: n/a
On 16 Nov 2006 18:41:39 -0800, "wh*****@gmail.com" <wh*****@gmail.com>
wrote in comp.lang.c:
>
"Tor Rustad д
"
in************@gmail.com wrote:
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 <identifierhas 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;
}

--
Tor <torust AT online DOT no>


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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 17 '06 #13

P: n/a
wh*****@gmail.com wrote:
"Tor Rustad д
"
in************@gmail.com wrote:
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 <identifierhas to be a macro name, else the
undef will be ignored. However, this works:
<snip>
#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.
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;
}
Nov 17 '06 #14

P: n/a
Jack Klein wrote:
>
On 16 Nov 2006 18:41:39 -0800, "wh*****@gmail.com" <wh*****@gmail.com>
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.
#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.
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:Th*************@gmail.com>
Nov 18 '06 #15

P: n/a
santosh wrote:
>
wh*****@gmail.com 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:Th*************@gmail.com>

Nov 18 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.