Thread: Malloc a struct pointer inside an array of structs

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    21

    Malloc a struct pointer inside an array of structs

    How do I malloc this?

    Code:
    #ifndef HASHTABLE
    #define HASHTABLE
    
    
    #define WORD_LENGTH 45
    #define TABLE_LENGTH 26
    
    
    #include <stdlib.h>
    
    
    typedef struct node 
    {
        char word[WORD_LENGTH + 1];
        struct node *next;
        
    } node;
    
    
    typedef struct table_node 
    {
        char letter;
        struct node *next;
        
    } table_node;
    
    
    void create_hash_t( );
    
    #endif
    Code:
    #include "hashTable.h"
    
    
    table_node hashtable[TABLE_LENGTH];
    
    
    
    
    void create_hash_t( )
    {
        for (int i = 0; i < TABLE_LENGTH; i++)
        {
            if ((hashtable->next[i] = malloc(sizeof(table_node)) == NULL)) //RIGHT HERE
            { }
          
        }
    }
    Thanks.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You need to be more clear about what your problem is. Compiler error? Please copy-paste the entire error message with line number. Runtime error? Please post input to reproduce, expected and actual outputs, and any bizarre behavior.

    1. hashtable is an array. hashtable->next is not.
    2. What kind of pointer is next? What does it point to? How much memory should you allocate? Hint sizeof(table_node) is wrong.
    3. I usually find it easier and more readable to separate the malloc line from the NULL check.

    Note, a common idiom for using malloc is
    Code:
    Foo *bar;  // bar is of type pointer to Foo
    bar = malloc(sizeof(*bar));
    if (bar == NULL)
    If bar is what you're allocating to, then using *bar in the sizeof ensures you always allocate the right amount of memory. Even if you change the type of bar, the malloc will still be correct.

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    21
    Here is the error:
    Code:
    !arortell@gentoo ~/Development/Projects/C_Projects/Data_Structures/HashTable $ make
    clang -ggdb3 -O0 -Qunused-arguments -std=gnu99 -Wall -Werror   -c -o hashTable.o hashTable.c
    hashTable.c:23:27: error: assigning to 'struct node' from incompatible type 'int'
                    if ((hashtable->next[i] = malloc(sizeof(table_node)) == NULL))
                                            ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1 error generated.
    <builtin>: recipe for target 'hashTable.o' failed
    make: *** [hashTable.o] Error 1
    Next is a pointer to the next node. I have two types of nodes one to hold the word and a pointer to the next node. The table nodes hold the letter and a pointer to the nodes. I wanted to have an array of table nodes each holding a letter and a pointer to the nodes that hold the corresponding words 'A' points to words that start with 'A' and so on. I need to load a dictionary file into memory for a spell checker.

  4. #4
    Registered User
    Join Date
    Apr 2014
    Posts
    21
    Ok I fixed that here is what I have now.

    Code:
    void create_hash_t( )
    {
    	hashtable = malloc(TABLE_LENGTH * sizeof(table_node));
    	
    	for (int i =0; i < TABLE_LENGTH; i++)
    	{
    		if ((hashtable->next[i] = malloc(sizeof(*node)) == NULL))
    		{
    			
    		}
    	}
    }
    Code:
    !arortell@gentoo ~/Development/Projects/C_Projects/Data_Structures/HashTable $ make
    clang -ggdb3 -O0 -Qunused-arguments -std=gnu99 -Wall -Werror   -c -o hashTable.o hashTable.c
    hashTable.c:23:44: error: unexpected type name 'node': expected expression
                    if ((hashtable->next[i] = malloc(sizeof(*node)) == NULL))
                                                             ^
    1 error generated.
    <builtin>: recipe for target 'hashTable.o' failed
    make: *** [hashTable.o] Error 1

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You're treating next[i] as if it were an array. But it is only a pointer at this point. However, hashtable is an array at this point so you want to dereference it as such

    Code:
    hashtable[i].next
    Anduril, check me on this one, it's been a while since I did anything in C

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I told you, hashtable->next is not an array. That means doing things like [ ] for array indexing is wrong, so hashtable->next[i] is wrong. Even if you had already allocated hashtable->next as an array, it wouldn't be an array of pointers, so you couldn't assign the result of malloc to it.

    hashtable is the array. That is what needs the [index].

    Notice that my example of malloc put the variable name in the sizeof with a * in front of it, not a type name.

    How do you plat on returning the malloc'ed hashtable back to the calling function?

    @camel-man: looks about right

  7. #7
    Registered User
    Join Date
    Apr 2014
    Posts
    21
    I tried this.

    Code:
    table_node hashtable[TABLE_LENGTH];
    
    
    
    
    void create_hash_t( )
    {
    	//hashtable = malloc(TABLE_LENGTH * sizeof(table_node));
    	
    	for (int i =0; i < TABLE_LENGTH; i++)
    	{
    		if ((hashtable[i].next = malloc(sizeof(*node)) == NULL))
    		{
    			
    		}
    	}
    }
    And her is what I get now
    Code:
    !arortell@gentoo ~/Development/Projects/C_Projects/Data_Structures/HashTable $ make
    clang -ggdb3 -O0 -Qunused-arguments -std=gnu99 -Wall -Werror   -c -o hashTable.o hashTable.c
    hashTable.c:15:43: error: unexpected type name 'node': expected expression
                    if ((hashtable[i].next = malloc(sizeof(*node)) == NULL))
                                                            ^
    1 error generated.
    <builtin>: recipe for target 'hashTable.o' failed
    make: *** [hashTable.o] Error 1
    I tried to use sizeof(hashtable.next) like you said instead of the type but I get this.
    Code:
    !arortell@gentoo ~/Development/Projects/C_Projects/Data_Structures/HashTable $ make
    clang -ggdb3 -O0 -Qunused-arguments -std=gnu99 -Wall -Werror   -c -o hashTable.o hashTable.c
    hashTable.c:15:52: error: member reference base type 'table_node [26]' is not a structure or union
                    if ((hashtable[i].next = malloc(sizeof(*hashtable.next)) == NULL))
                                                            ~~~~~~~~~^~~~~
    1 error generated.
    <builtin>: recipe for target 'hashTable.o' failed
    make: *** [hashTable.o] Error 1

  8. #8
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Quote Originally Posted by anduril462 View Post

    Notice that my example of malloc put the variable name in the sizeof with a * in front of it, not a type name.

    How do you plat on returning the malloc'ed hashtable back to the calling function?
    Re-read what Anduril said

    Edit: Rememeber that hashtable is an array. you are not indexing it like an array though.
    Last edited by camel-man; 10-22-2014 at 02:39 PM.
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  9. #9
    Registered User
    Join Date
    Apr 2014
    Posts
    21
    I'm sorry I don't understand.

    Code:
     if((hashtable[i].next = malloc(sizeof(*hashtable.next)) == NULL))
    hastable.next is the variable name and I get the second error message above.
    Last edited by arortell; 10-22-2014 at 02:44 PM.

  10. #10
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    look at this closely
    Code:
    sizeof(*hashtable.next)
    compare it to all the other times youve referenced hashtable, look at previous posts on what was being said about hashtable being referenced like that.
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  11. #11
    Registered User
    Join Date
    Apr 2014
    Posts
    21
    I got it thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-30-2012, 08:12 AM
  2. Pointer inside a struct - how to access it?
    By farukyaz in forum C Programming
    Replies: 15
    Last Post: 10-15-2011, 12:58 AM
  3. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  4. Pointer to union inside struct
    By lv2eof in forum C Programming
    Replies: 4
    Last Post: 05-28-2010, 06:55 PM
  5. Problem with DMA inside a struct pointer
    By Nakor in forum C Programming
    Replies: 5
    Last Post: 09-15-2007, 08:14 PM