How to undef a typedef??

Discussion in 'C Programming' started by informativeguy@gmail.com, Nov 16, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Chris Dollin Guest

    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/
     
    Chris Dollin, Nov 16, 2006
    #2
    1. Advertising

  3. Eric Sosman Guest

    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
     
    Eric Sosman, Nov 16, 2006
    #3
  4. Tor Rustad Guest

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

    --
    Tor <torust AT online DOT no>
     
    Tor Rustad, Nov 16, 2006
    #4
  5. 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:>
     
    Kenneth Brody, Nov 16, 2006
    #5
  6. osmium Guest

    "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?
     
    osmium, Nov 16, 2006
    #6
  7. Flash Gordon Guest

    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.

    >> 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
     
    Flash Gordon, Nov 16, 2006
    #7
  8. osmium Guest

    "Flash Gordon" <> wrote in message
    news:-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.
    >
    > >> 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.
     
    osmium, Nov 16, 2006
    #8
  9. John Bode Guest

    Eric Sosman wrote:
    > 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.
     
    John Bode, Nov 16, 2006
    #9
  10. Random832 Guest

    2006-11-16 <>,
    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.
     
    Random832, Nov 16, 2006
    #10
  11. John Bode wrote:
    > Eric Sosman wrote:
    > > 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
     
    Peter Nilsson, Nov 16, 2006
    #11
  12. Guest

    "Tor Rustad дµÀ£º
    "
    > 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 <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;
    > }
    >
    > --
    > 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, 2006
    #12
  13. Jack Klein Guest

    On 16 Nov 2006 18:41:39 -0800, "" <>
    wrote in comp.lang.c:

    >
    > "Tor Rustad дµÀ£º
    > "
    > > 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 <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;
    > > }
    > >
    > > --
    > > 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/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Nov 17, 2006
    #13
  14. santosh Guest

    wrote:
    > "Tor Rustad дµÀ£º
    > "
    > > 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 <identifier> has 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;
    > }
     
    santosh, Nov 17, 2006
    #14
  15. Jack Klein wrote:
    >
    > On 16 Nov 2006 18:41:39 -0800, "" <>
    > 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:>
     
    Kenneth Brody, Nov 18, 2006
    #15
  16. santosh wrote:
    >
    > 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:>
     
    Kenneth Brody, Nov 18, 2006
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vikas Yadav
    Replies:
    0
    Views:
    1,127
    Vikas Yadav
    Sep 9, 2003
  2. Sachin

    #undef for function

    Sachin, Nov 25, 2005, in forum: C Programming
    Replies:
    3
    Views:
    422
    Peter Ammon
    Nov 28, 2005
  3. Klaus Ahrens
    Replies:
    6
    Views:
    382
    Alf P. Steinbach
    Nov 29, 2005
  4. oor
    Replies:
    0
    Views:
    1,357
  5. Tim McDaniel

    undef($foo) versus $foo = undef()?

    Tim McDaniel, Aug 19, 2009, in forum: Perl Misc
    Replies:
    6
    Views:
    144
    Peter J. Holzer
    Aug 19, 2009
Loading...

Share This Page