Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically sized array of pointers to linked lists
    primarykey
    data
    text
    <p>I'm trying to figure out how to initialize an array of pointers to linked lists that has a dynamic size based on user input. </p> <p>I've got a struct as follows:</p> <pre><code>struct HashTable { int tableSize; int (*getKey)(void *); char * (*toString)(void *); void (*freeHashObject)(void *); Boolean (*compare)(void *, void *); ListPtr table; }; </code></pre> <p>The table variable is supposed to contain a dynamic number of linked lists depending on user input. You can assume that n is a valid integer and that createHashObject works as it is supposed to.</p> <pre><code>HashTablePtr table; HashObjectPtr job; table = createHashTable(n, getKey, toString, freeHashObject, compare); for (i=0; i&lt;n; i++) { job = createHashObject(firstInput); HashInsert(table, job); } </code></pre> <p>I believe the problem lies in createHashTable which is as follows.</p> <pre><code>HashTablePtr createHashTable(int size, int (*getKey)(void *), char * (*toString)(void *), void (*freeHashObject)(void *), Boolean (*compare)(void *, void *)) { HashTablePtr h = (HashTablePtr)malloc(sizeof(HashTable)); //dont worry that i dont do things with the function pointers h -&gt; table = (ListPtr)malloc(sizeof(List)*size); int i; for (i = 0; i &lt; size; i++) { h -&gt; table[i] = createList(getKey, toString, freeHashObject); } } </code></pre> <p>The above code doesn't seem to correctly initialize all the linked lists. Here is the code for the createList function and the List struct.</p> <pre><code>ListPtr createList(int(*getKey)(void *), char * (*toString)(void *), void (*freeHashObject)(void *)) { ListPtr list; list = (ListPtr) malloc(sizeof(List)); list-&gt;size = 0; list-&gt;head = NULL; list-&gt;tail = NULL; list-&gt;getKey = getKey; list-&gt;toString = toString; list-&gt;freeObject = freeHashObject; return list; } struct list { int size; NodePtr head; NodePtr tail; int (*getKey)(void *); char * (*toString)(void *); void (*freeObject)(void *); }; </code></pre> <p>I am willing to wager that the issue lies in how I am defining the dynamically sized array of pointers to the linked lists and then initializing it. Can anyone offer me some help with that?</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload