title: C - Undeclared variable at declaration
body: <p>I'm building a hash table in C. 
  Everything seems to work fine except this variable, <code>cur_item</code>, that report me an error at compilation.</p>
  
  <p>Here is the code :</p>
  
  <pre><code>void insert(hash_table* ht, const char* key, const char* value) {            
     const int load = ht-&gt;count * 100 / ht-&gt;size;                               
     if (load &gt; 70) {                                                           
       resize_up(ht);                                                           
     }                                                                          
     item* item = new_item(key, value);                                         
     int index = get_hash(item-&gt;key, ht-&gt;size, 0);                              
     item* cur_item = ht-&gt;items[index];                                         
     int i = 1;                                                                 
     while (cur_item != NULL) { 
       /** PROCESS **/
     }
  }
  </code></pre>
  
  <p>Here is my Makefile (not the best one I guess):</p>
  
  <pre><code>main: main.o hash_table.o prime.o                                            
            gcc  -g -Wall -lm -o main.out ./build/main.o ./build/hash_table.o ./\
     build/prime.o                                                                
  
  main.o: ./src/main.c ./src/hash_table.h                                      
            gcc -c  ./src/main.c -o ./build/main.o                               
  
  hash_table.o: ./src/hash_table.c ./src/hash_table.h                          
           gcc -c  ./src/hash_table.c -o ./build/hash_table.o                   
  
  prime.o: ./src/prime.c ./src/prime.h                                         
           gcc -c  ./src/prime.c -o ./build/prime.o    
  </code></pre>
  
  <p>And here is the error :</p>
  
  <pre><code>./src/hash_table.c: In function ‘insert’:
  ./src/hash_table.c:65:9: error: ‘cur_item’ undeclared (first use in this function); did you mean ‘del_item’?
     item* cur_item = ht-&gt;items[index];
           ^~~~~~~~
           del_item
  ./src/hash_table.c:65:9: note: each undeclared identifier is reported only once for each function it appears in
  make: *** [Makefile:10: hash_table.o] Error 1
  </code></pre>
  
  <p>The type <code>item</code> represent a structure I created.
  <code>del_item</code> is a function, no reason to use it there.</p>
  
  <p>If I declare <code>cur_item</code> before the <code>if</code> loop and then initialize its value after the <code>get_hash()</code> function, the compilation works fine.</p>
  
  <p>Can someone explain me why the first compilation failed ? Is there something missing in my Makefile ?</p>