Thread: typeundef???

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Smile typeundef???

    I know this is a silly question: Is there a way to undefining user defined type?

    Example:

    Code:
    typedef struct Node
    {
        int data;
        struct Node *next;
    
    } Node;
    
    ...your singly-linked list code here...
    
    typeundef Node;
    
    typedef struct Node
    {
        int data;
        struct Node *previous;
        struct Node *next;
    } Node;
    
    ...your doubly-linked list code here...
    Any trick exists? (~^_^~)

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Why would you wanna do that?

    BTW,
    Doubly linked-list? What do you mean?
    Last edited by eXeCuTeR; 07-02-2008 at 04:26 PM.

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    I know that you can do #undef macroname, but I am not sure if it would work with structs.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,226
    That question struck me quite a while ago, too. The answer I found is "no".

  5. #5
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    BTW,
    Doubly linked-list? What do you mean?
    An interesting linear data structure:
    http://en.wikipedia.org/wiki/Linked_list

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by audinue View Post
    An interesting linear data structure:
    http://en.wikipedia.org/wiki/Linked_list
    I know what is a linked-list, I implement these generic data structures (stacks, binary trees, queues) in C#, but I always use a single linked-list, not a doubly, it's not necessary since you can do the same thing with a single one.

    cyberfish --> Yep, kinda figured. actually my questions referred to its necessity more than what it is (could understand from the code)
    Last edited by eXeCuTeR; 07-02-2008 at 04:57 PM.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,226
    one in which each node contains BOTH a pointer to the next node AND a pointer to the previous node. That way you can traverse the list both ways.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There's no way to undo a typedef. Only macros.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    So, what is the best way to define data structures to avoid collision with another?

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Collisions aren't likely unless you come up with bland names. You could always come up with a decent naming convention if you're implementing a library.

  11. #11
    ---
    Join Date
    May 2004
    Posts
    1,379
    But is it possible to redefine a user defined type?
    Code:
    typedef struct Node
    {
        int data;
        struct Node *next;
    
    } Node;
    
    // Use Node here
    
    typedef struct Node
    {
        int data;
        struct Node *previous;
        struct Node *next;
    } Node;
    
    // Use new definition here
    If this isn't possible (I don't have a compiler handy at the moment) then no big deal. Just use a different name. If the feature was really required in C, it would be implemented.

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I imagine that would kind of ruin typing for functions that accept typedef'ed arguments.

  13. #13
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    You'd run into two problems there, both the redef of "struct Node" and its re-typedef. Either way you slice it, unique names should be used.
    The best you could hope for is to hack it with the pre-processor:
    Code:
    #define Node struct _Node1
    struct _Node1
    {
        int data;
        struct _Node1 *next;
    
    } ;
    
    // Use Node here
    
    #undef Node
    #define Node struct _Node2
    struct _Node2
    {
        int data;
        struct _Node2 *previous;
        struct _Node2 *next;
    } ;
    
    // Use Node here
    if you were say merging code and absolutely needed to preserve the "Node" identifier.

  14. #14
    ---
    Join Date
    May 2004
    Posts
    1,379
    Wasted effort.
    Just use different names.
    Besides, if you are implementing a double-linked list, what use is a single-linked list?

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A single-linked list offers less functionality but is more memory-efficient in what it does?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed