Note that there are some explanatory texts on larger screens.

plurals
  1. POWord count in C, learning more CS
    text
    copied!<p>After about 5 years of programming in dynamic languages such as Python and JS I am starting to feel I'm missing out of what happens under the hood. Languages such as these are really great because they let you focus on what you have to do leveraging the trouble of working with pointers, memory allocation and many searching, sorting, inserting algorithms. Even though I never regret using these languages as I really feel they are ridiculously powerful I feel that, in order to become a better programmer, I need to take a step back and understand what happens under the hood!</p> <p>I decided to do this by writing a simple word counter: The app gets all the params and outputs all the unique words, each one with a counter: "Hello world Hello" would return "Hello: 2", "world: 1" (not taking in consideration the actual output structure). This program is the Python equivalent of:</p> <pre><code>import sys from collections import defaultdict def main(): results = defaultdict(int) for word in sys.argv[1:]: results[word] += 1 print results </code></pre> <p>Writing it in C is a bit different, I feel like I'm getting something utterly wrong with pointers, arrays of pointers and all that stuff! I want to get better, Help me get better!!</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; // This is what a key-value pair: &lt;int, string&gt; typedef struct { int counter; unsigned char* word; } hashmap; // Checks if inside the array of results, hashmap-&gt;word is equals to word paramter hashmap* get_word_from_results(hashmap* results[], int count, const char* word) { int i; hashmap* result; for (i = 0; i &lt; count; i++) { result = results[i]; if (result-&gt;word == (unsigned char *)word) return result; } return NULL; } int main(int argc, const char *argv[]) { hashmap* results; int results_counter = 0; int i; const char* word; for (i = 1; i &lt; argc; i++) { word = argv[i]; hashmap* result = get_word_from_results(&amp;results, results_counter, word); // If result is NULL, means word is not inserted yet, let's create a new hashmap and insert it inside the array if (result == NULL) { hashmap h; h.counter = 1; h.word = (unsigned char *)word; results = realloc(NULL, (results_counter + 1) * sizeof(hashmap) ); // NOTE: potential memory leak? would h be deallocated? results[results_counter] = h; results_counter++; printf("NEW\n"); } else { // The word already exists in the hashmap array, let's increase it by 1 result-&gt;counter++; printf("INCREMENTED\n"); } } return 0; } </code></pre> <p>Can anyone give me some advice? what am I doing wrong here? Are my pointers okay? also I think I spotted a memory leak (see comments), would anyone like to submit their version??</p> <p>Thanks!! you guys are so cool!!</p> <p>Daniel</p>
 

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