Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the difference between intializating a struct as pointer or not?
    primarykey
    data
    text
    <p>I have the following for my HashTable structure:</p> <pre><code>typedef char *HashKey; typedef int HashValue; typedef struct sHashElement { HashKey key; HashValue value; } HashElement; typedef struct sHashTable { HashElement *items; float loadFactor; } HashTable; </code></pre> <p>I never really thought about it until now but I just realized there's two ways how I can use this:</p> <p><strong>Alternative 1:</strong></p> <pre><code>void hashInitialize(HashTable *table, int tabSize) { table-&gt;items = malloc(sizeof(HashElement) * tabSize); if(!table-&gt;items) { perror("malloc"); exit(1); } table-&gt;items[0].key = "AAA"; table-&gt;items[0].value = 45; table-&gt;items[1].key = "BBB"; table-&gt;items[1].value = 82; table-&gt;loadFactor = (float)2 / tabSize; } int main(void) { HashTable t1; int i; hashInitialize(&amp;t1, HASHSIZE); for(i = 0; i &lt; HASHSIZE - 1; i++) { printf("PAIR(%d): %s, %d\n", i+1, t1.items[i].key, t1.items[i].value); } printf("LOAD FACTOR: %.2f\n", t1.loadFactor); return 0; } </code></pre> <p><strong>Alternative 2:</strong></p> <pre><code>void hashInitialize(HashTable **table, int tabSize) { *table = malloc(sizeof(HashTable)); if(!*table) { perror("malloc"); exit(1); } (*table)-&gt;items = malloc(sizeof(HashElement) * tabSize); if(!(*table)-&gt;items) { perror("malloc"); exit(1); } (*table)-&gt;items[0].key = "AAA"; (*table)-&gt;items[0].value = 45; (*table)-&gt;items[1].key = "BBB"; (*table)-&gt;items[1].value = 82; (*table)-&gt;loadFactor = (float)2 / tabSize; } int main(void) { HashTable *t1 = NULL; int i; hashInitialize(&amp;t1, HASHSIZE); for(i = 0; i &lt; HASHSIZE - 1; i++) { printf("PAIR(%d): %s, %d\n", i+1, t1-&gt;items[i].key, t1-&gt;items[i].value); } printf("LOAD FACTOR: %.2f\n", t1-&gt;loadFactor); return 0; } </code></pre> <p><strong>Question 1:</strong> They both seem to produce the same result. On <code>main</code>, both examples print the right key/value pair. So, what exactly is the different between them besides the syntax change (using <code>(*table)</code> instead of just <code>table</code>), the extra code to allocate memory for the <code>HashTable</code> structure and the declaration of <code>HashTable</code> pointer?</p> <p>I've been writing a few data structures lately like stacks, linked lists, binary search trees and now hash tables. And for all of them, I've always used the alternative 2. But now I'm thinking if I could have used alternative 1 and simplify the code, removing most of the <code>*</code> and <code>&amp;</code> that are all over the place.</p> <p>But I'm asking this question to understand the differences between the two methods and if, and also why, I should use on over the other.</p> <p><strong>Question 2:</strong> As you can see in the structures code, <code>HashKey</code> is a pointer. However, I'm not using <code>strdup</code> nor <code>malloc</code> to allocate space for that string. How and why is this working? Is this OK to do? I've always used <code>malloc</code> or <code>strdup</code> where appropriate when handling dynamic strings or I would get lots of segmentation faults. But this code is not giving me any segmentation faults and I don't understand why and if I should do it like this.</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.
 

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