title: (C Programming) How do I use a data structure and function which are defined in another file?
body: <p>Say, I have a file <code>dataStructure.c</code> and <code>dataStructure.h</code>. (My data structure is a hash map.) These files contain the implementation of the data structure and methods for adding new entries to the structure as well as retrieving entries. </p>
  
  <p>Here's an example: </p>
  
  <pre><code>// dataStructure.h
  
  struct node {
      char *label;
      int address;
      struct node *next; // Points to the next node.
  };
  
  struct ourTable {
      int size;
      struct node **list; // List of all the (key, value) pairs.
  };
  
  // Then, here are methods to create table, add new entries and retrieving them.
  struct ourTable createTable(int size);
  
  void addEntry(struct ourTable *t, char *label, int address);
  
  unsigned retrieveAddress(struct ourTable* table, char *label);
  </code></pre>
  
  <p>The function <code>retrieveAddress</code> basically just returns the address of that label. Since I am trying to implement a hash map, which is just a data structure of several (key, value) pairs. In my case, the key is <code>label</code> whereas value is <code>address</code>.</p>
  
  <pre><code>unsigned retrieveAddress( struct ourTable* table, char *label)
  {
      int bin = 0;
      bin = hashFunction(table, label); // Hashing function
  
      struct node *list = table-&gt;list[bin];
      struct node *entryItem = list;
      while(entryItem)
      {
          if (entryItem-&gt;label == label)
          {
              return entryItem-&gt;address; // Returns the address of that label.
          }
          entryItem = entryItem-&gt;next;
      }
      return NULL;
  }
  </code></pre>
  
  <p>Then, I have another file, <code>establishTable.c</code> which just uses the methods implemented in <code>dataStructure.h</code> to create a table and then add new entries. This is what I wrote in that file:</p>
  
  <pre><code>// establishTable.c
  
  #include "dataStructure.h"
  
  struct ourTable establishTable()
  {    
      struct ourTable table = createTable(1000); // Create a table with a maximum of 1000 entries.
      addEntry(&amp;table, "word", 1234);
  }
  </code></pre>
  
  <p>And what I want to do is to pass the structure <code>ourTable</code> with the new entries I inserted in <code>establishTable.c</code> to the main file <code>main.c</code>. To illustrate what I am trying to accomplish:</p>
  
  <pre><code>// main.c
  
  #include "dataStructure.h"
  
  #include "establishTable.h"
  
  int main()
  {
      // I'm not sure how to pass the data structure... Something like this:
      struct ourTable table = establishTable(); 
  
      // Get the retrieveLabel function from dataStructure.h and use it here.
      printf("retrieved from the table at %u\n\n", retrieveAddress(&amp;table,"word") );
  }
  </code></pre>
  
  <p>I tried running the <code>main.c</code>. It doesn't show any errors but it just outputs </p>
  
  <pre><code>retrieved from the table at 0
  </code></pre>
  
  <p>which just tells me that the table I already established was not passed on at all. The output should be 1234.</p>
  
  <p>So, how do I pass a data structure and the result of a function from another file to my <code>main.c</code> file? It works when I just do everything in <code>establishTable.c</code> but that is not my intention. I have tried the <code>extern</code> method as suggested in other threads, but nothing works. </p>