Or does that automatically do that when I allocate memory to the nodeList[5].
No, you need to do that.
[edit]
Except that this won't go far: sizeof(void) -- you need a type to have a size of that type.
[/edit]
Why the casts on malloc ? If you're using C++, why not new or a vector ? If C, stop doing that and let your compiler help you write better code.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
node[5]->key = key;
That is wrong syntax. you have to use dot notation, not pointer notation because node[5] is NOT a pointer.
node[5].key = key;
If done this way I don't have to allocate memory to node[5]->key before setting it equal to key right?
Well, maybe yes and maybe no. How is paramater key being allocated? Will the calling function reuse that memory for something else, such as does it get overwritten each time the insert function is called or is new memory allocated each time?
I find it normally best to reallocate the memory so that node has complete control over it and not count on some other function allocating/deallocating node's memory.
eg. void *insert(void *key, long keysize)
{
node[5].key = malloc(keysize);
memcpy(node[5].key,key,keysize);
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343