title: Pass struct by reference in C, but modifications aren't all there
body: <p>I am trying to make a simple program to read a config file in C. But the data in my struct members isn't staying where I put it.</p>
  
  <p>Code at the bottom.</p>
  
  <p>This is what I <em>think</em> I am doing:</p>
  
  <ul>
  <li>I am creating a struct that </li>
  <li>Passing the struct by reference</li>
  <li>Populating the members of the struct from the config file</li>
  </ul>
  
  <p>When I try to access the members, I can only get the data from the last row in the config file.</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p>Edit: I expected it to populate the struct with key / values from the config file. Instead it only had the value of the last line in the config file.</p>
  
  <p><strong>expected output</strong></p>
  
  <p><code>example.com</code></p>
  
  <p><strong>actual output</strong></p>
  
  <p><code>food.txt</code></p>
  
  <p>Problem is with:</p>
  
  <pre><code>conf-&gt;key[i]   = key;
  conf-&gt;value[i] = value;
  </code></pre>
  
  <p><strong>main.c</strong></p>
  
  <pre><code>/*
  * Goal is to make simple key value storage with struct and pointers
  */
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #define MAXLEN 100
  #define CONFIG_MAX_BUF 1024
  #define DELIM "="
  
  struct config_s {
      char *key[MAXLEN];
      char *value[MAXLEN];
  };
  
  void read_config_file(struct config_s *conf, char *filename, int *size_of_array);
  
  int main() {
      int i = 0, size_of_array;
      // type config_s has key and value members
      struct config_s c;
  
      read_config_file(&amp;c, "test.cfg", &amp;size_of_array);
      printf("size of array: %i\n", size_of_array);
  
      // if i want to print them all out, i can use this, but i'll have 
      // a function to search for a certain key and return the value.
      int l = 0, e = 1;
      printf("********** TEST 1 ***********\n");
      printf("key: [%s] value: [%s] i: [%i]\n", c.key[l], c.value[l], l);
      printf("key: [%s] value: [%s] i: [%i]\n", c.key[e], c.value[e], e);
      printf("********** TEST 2 ***********\n");
  
      return 0;
  }
  
  void read_config_file(struct config_s *conf, char *filename, int *size_of_array) {
      FILE *file;
      file = fopen(filename, "r");
  
      char line[CONFIG_MAX_BUF];
  
      int i = 0;
  
      char *cfline;
      char *key;
      char *value;
  
      while(fgets(line, sizeof(line), file) != NULL) {
          cfline = strstr((char *)line, DELIM);
          value = cfline + strlen(DELIM);
          value[strlen(value) - 1] = '\0';
  
          key = strtok((char *)line, DELIM);
          printf("%i %s %s\n", i, key, value);
          conf-&gt;key[i]   = key;
          conf-&gt;value[i] = value;
          printf("%i %s %s\n", i, (*conf).key[i], (*conf).value[i]);
          i++;
      }
      printf("%s %s\n", (*conf).key[0], (*conf).value[0]);
      fclose(file);
      *size_of_array = i;
      return;
  }
  </code></pre>
  
  <p><strong>test.cfg</strong></p>
  
  <pre><code>server=example.com
  port=443
  file=food.txt
  </code></pre>