Thread: Uninitialized value of char *

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    5

    Uninitialized value of char *

    Hi there,

    I am doing some coursework in which I have to create a basic hash table. I am just experimenting at the moment, but I have made the hash function and as my hashtable, I want to have an array of the following Hashrow struct:

    Code:
    typedef struct l  {
        char *data;
    	struct l *next;
    } List;
    
    typedef struct h  {
        List l;
    } Hashrow;
    So basically when I initialise my hashtable, it is just an array of my Hashrow elements.

    So first I put a sample word through my hash function which returns a hash address. Then I send the word, the hash address (index of hash table) and a pointer to my hash table up to my insertword() function:

    Code:
    Hashrow *insertword(char *key, int hashaddress, Hashrow *hashtable)  {
        hashtable[hashaddress].l.data = key;
    	
    	return hashtable;
    }
    I have tested to see if the word has been added and it has. Cool!

    My problem is that I want to be able to check the 'data' member of the list at a certain index of the hash table, and if nothing has been added then fine that's all I want to know. I thought I could do this by something like:

    Code:
    char *blah = hashtable[11].l.data;
    if(blah == NULL)  
        printf("nothing there");
    but it doesn't work.

    As the 'data' member is a pointer to char, then it is an address which has not be assigned yet (erm.. I think). Do I need to initialise all the data members of my lists when initialising the hash table?

    I could do with finding out what the value of the uninitialsied
    Code:
    char *data;
    is so that I can make an 'if(blah == blah)' test to check that there is nothing in that index of the hash table.

    This is so that I can decide whether toadd the word directly or whether I need to create another List element for direct chaining.

    I hope I've made sense. If I could learn just one thing from a reply, then it would be what is the unititialised value of char *data.

    Many thanks

    Joe

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by joelem View Post
    As the 'data' member is a pointer to char, then it is an address which has not be assigned yet
    Yes, but that is not the same thing as NULL. NULL is an assignment.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    5
    Hi MK27,

    Thanks for your reply :-)

    Do you think a solution would be to assign all the 'data' members to NULL when I initialize the hash table? So I could then check to see if they are still null by:

    Code:
    if(hashtable[11].l.data == NULL)
        printf("Yup. It's NULL");
    Or do you know a way that I can just tell if the 'data' member is uninitialized? So I would still be able to use an if statement, but I wouldn't be checking if it == NULL, but something else.

    Many thanks

    Joe

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You can't tell if something is uninitialized. You should initialize your pointers to NULL before the hash table is used.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Your pointer data will point to some random spot in memory. So referencing that will bring doom to the word. You will need to initialize your pointer prior to referencing it.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by joelem View Post
    Do you think a solution would be to assign all the 'data' members to NULL when I initialize the hash table?
    Yes. As bithub says, there is no way to tell if something is uninitialized.

    A related tip: if you free() a ptr and keep the handle, set that handle to NULL so you can tell it has been de-allocated, since there is no way to tell if something has been free'd, either.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    5
    OK. Cool! I will initialise the pointers to NULL.

    Thank you all very much for your advice and time.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM