Thread: typedef question

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    typedef question

    can I have two typedef with the same name but different things inside of it?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by -EquinoX- View Post
    can I have two typedef with the same name but different things inside of it?
    You cannot have two typedefs with the same name, period.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Sure, as long as they aren't in the same file and never get #included together.
    Why would you want to? That would be very confusing.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    hmm.. can I cast a typedef? it's basically just a pointer right?

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by -EquinoX- View Post
    hmm.. can I cast a typedef? it's basically just a pointer right?
    No. A typedef names a type. It has nothing to do with pointers or any other kind of variable. What are you trying to do?

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so I have this:

    Code:
    struct node_t{
      char* word;
      bla.. bla.. bla
    }
    
    typedef struct node_t Words;
    when I call LinkedListConstruct it returns Words*, when I call HashTableConstruct I want it to return a similar thing too.. Words*, but my hashtable function returns a HashTable* which is a different kind of struct from Words*

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by -EquinoX- View Post
    so I have this:

    Code:
    struct node_t{
      char* word;
      bla.. bla.. bla
    }
    
    typedef struct node_t Words;
    when I call LinkedListConstruct it returns Words*, when I call HashTableConstruct I want it to return a similar thing too.. Words*, but my hashtable function returns a HashTable* which is a different kind of struct from Words*
    So why not call one LinkedListNode and the other HashTableNode?

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I can't because I have a file that I can't change and that file does:

    Words* f = DictionaryConstruct(some argument here); <== I can't change this!

    and inside dictionary construct I have two types of construct, one is a hashtable construct and a bst construct.. which both returns a different type of struct.. and on the other hand I need to return a pointer to a struct called *Words

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I mean when constructing a linked list and a bst, you got two different things going on at the same time:

    in a struct of a linked list I have the next node (which is basically another struct inside a struct) I have the data itself. in a bst in the struct I have a left and right tree which is another struct inside a struct and the data

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So what you're really asking is "can I make a function return two different things, depending on the phase of the moon and other unrelated things?" The answer is no -- DictionaryConstruct must always return the same type; it should never return sometimes a hashtable and sometimes a search tree. Pick one.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    hmm.. so I can only return one? okay then, I'll have to figure out another way through this then.. by the way if I am implementing a hashtable, how can I get all the elements in that hash table if I don't know the key? is there a way to traverse a hash table?

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    hmm..let me explain my situation here more clearly:

    so I have a code optimization assignment to be done. The program basically just takes two files,
    first it stores the list of words in a data structure unsortedly in a hash table. Second it takes the second file, which is also a list of words. Before storing the second list of words into a binary tree I want to check first if that word exists in the first file, by checking the hash table. If it does exist then I put that word in a binary tree. In other words, two similar words that exists on both files are put inside a binary tree.

    The problem is I have this code that I can't change or do anything to it:

    Words* fd = Construct(..)

    Words is just a pointer to a struct evertime it constructs that list of words file into a linked list (the current implementation is a linked list). But my HashTable returns a pointer to a struct which supports a hash table, a struct with key and values inside and my BinaryTree returns a pointer to a struct which is a binary tree struct/node that has a left and right subtree.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    On the assumption your hash table is an array of linked lists, you can walk each linked list in order.

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    yes, my hash table is an array of linked list.. so I have to check each array slot it there's a data in that linked list? I guess this wouldn't be so efficient right

    is there a way to iterate over a hash table and get the data one by one in a sorted way?

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by -EquinoX- View Post
    yes, my hash table is an array of linked list.. so I have to check each array slot it there's a data in that linked list? I guess this wouldn't be so efficient right

    is there a way to iterate over a hash table and get the data one by one in a sorted way?
    Unless your array of lists is somehow exponentially larger than it needs to be, traversal of a hash table is just as efficient of traversal of anything else; most every traversal is O(n) since you have to visit every item once.

    Unless your hashing mechanism and list-inserting mechanism are very carefully designed, traversal of a hash table will not be in sorted order.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with ZwQueryDirectoryFile for my Driver.
    By taDo in forum Windows Programming
    Replies: 9
    Last Post: 11-27-2008, 08:54 AM
  2. Error: redefinition of typedef xxxx
    By Rocha in forum C Programming
    Replies: 2
    Last Post: 11-24-2008, 09:19 AM
  3. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  4. Help...typedef struct...confusing...
    By darkchild in forum C Programming
    Replies: 1
    Last Post: 01-23-2007, 08:03 AM
  5. question about typedef
    By volk in forum C++ Programming
    Replies: 8
    Last Post: 05-30-2003, 10:53 PM