0

I've read an answer to a similar question about

node* new_node = malloc(sizeof(node)); 

and how it allocates memory for the pointer plus all the data contained within the struct as it was declared

typedef struct node
{
    char word[LENGTH +1]
    struct node* next;
}
node;

But when I create an array for my hash-table, I just want the indices to contain only pointers, and not to allocate memory for the structs that these pointers point to.

Does node* hashtable[500]; create an array of pointers or does it create an array of nodes ?

If the latter is true, how do I create the hash-table so that the indices of the array contain pointers that do not have memory allocated for them. Is the following code useful?

typedef node *link
node;

link *hashtable[500];

1 Answer 1

1

Does node* hashtable[500]; create an array of pointers or does it create an array of nodes ?

It creates an array of node *s (otherwise known as pointers to nodes). The array declaration above does NOT initialize any of the pointers in the array. Therefore, it does not allocate memory for any of them.

So if you tried to access hashtable[10] for example, you would get a pointer to a node. You could then do something like

// allocate memory for hashtable[10]
hashtable[10] = malloc(sizeof (node));

Is the following code useful?

Not really. I'm not sure your code will even compile. If you rather meant something like

typedef node * link;
link *hashtable[500];

then in this case, you're defining link as a type synonym to node * which makes link * equal to node ** (i.e., a pointer to a pointer to a node) and I'm sure you don't want this.

10
  • Okay I see, so node* new_node = malloc(sizeof(node)) initializes the pointer and the struct node, and then you set the pointer at hashtable[10] equal the the pointer to the struct node. So new_node is the pointer variable, and new_node->word or new_node->next are a separate entity that contain the struct. In fact, new_node->next is node ** next? But I wouldn't be able to access its contents with node ** next, right? Commented Dec 25, 2014 at 14:07
  • I guess the language is a bit redundant which makes it confusing. Since node is a type (or a class?), when I malloc(sizeof(node)), what would it malloc if I didn't declare a struct called node? And how does the compiler know the difference? If I called it typedef node data, and then I node new_node = malloc(sizeof(node)), what would that do? Commented Dec 25, 2014 at 15:17
  • @ErinMagner Be careful with the terminology. You initialize the pointer with the memory address returned by malloc which allocates memory of the node size. malloc NEVER initializes the struct, it just allocates memory for it. When you allocate memory for a node to be pointed to by a pointer (e.g., hashtable[10] or new_node, word and next are members of the structs that are pointed to by these pointers. new_node->next in this case is of type node * NOT node **. C is a structured language, there are no classes in C.
    – kzidane
    Commented Dec 25, 2014 at 17:42
  • @ErinMagner malloc takes the size you want it to allocate in bytes, allocates memory of this size and returns a pointer (an address) to this memory block. Execute man malloc in the terminal for more information!
    – kzidane
    Commented Dec 25, 2014 at 17:44
  • When you allocate memory for a node to be pointed to by a pointer (e.g., hashtable[10] or new_node, word and next are members of the structs that are pointed to by these pointers. Now I am extremely confused, because my original question was about how I can initialize my hashtable so that it would not allocate memory for the structs as well as the pointers. Commented Dec 25, 2014 at 18:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .