title: SOS I am crazy but haven't found the reason why the line 137 doesn't work
body: <p>The code is intended to read the each line from a CSV file (which is separated by comma), and then insert each line as a value into a binary tree.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;assert.h&gt;
  #include &lt;string.h&gt;
  
  #define MAX_CHAR 128
  #define MAX_LINE 512
  #define COMMA ","
  
  typedef struct node node_t;
  typedef struct {
      node_t *root;
      int (*cmp)(void *, void *);
  } tree_t;
  
  typedef struct {
      char id[MAX_CHAR];
      char sex[MAX_CHAR];
      char age[MAX_CHAR];
      char height[MAX_CHAR];
      char weight[MAX_CHAR];
      char team[MAX_CHAR];
      char noc[MAX_CHAR];
      char games[MAX_CHAR];
      char year[MAX_CHAR];
      char season[MAX_CHAR];
      char city[MAX_CHAR];
      char sport[MAX_CHAR];
      char event[MAX_CHAR];
      char medal[MAX_CHAR];
  } data_t;
  
  typedef struct {
      char *name;
      data_t data;
  } athlete_t;
  
  struct node {
      athlete_t *data;
      node_t *left;
      node_t *right;
  };
  
  tree_t *make_empty_tree(int func(void *, void *));
  void insert_to_struct(athlete_t *, char *);
  tree_t *read_file(char*, tree_t *);
  int cmp(void *p1, void *p2);
  tree_t *insert_in_order(tree_t *, athlete_t *);
  node_t *recursive_insert(node_t *, node_t *, int cmp(void *, void *));
  void traverse_tree(tree_t *, void action(void *));
  void recursive_traverse(node_t *, void action(void *));
  void action(void *);
  void ini_struct(athlete_t *);
  
  int main(int argc, char **argv) {
      tree_t *tree = make_empty_tree(cmp);
      tree = read_file(argv[1], tree);
      printf("%s\n", tree-&gt;root-&gt;data-&gt;name);
      traverse_tree(tree, action);
      return 0;
  }
  
  /* Read each line from a file, and assume max length of line is 512 chars*/
  tree_t *read_file(char *filename, tree_t *tree) {
      FILE *fp_data;
      char new_line[MAX_LINE];
      athlete_t *data_struct;
  
      fp_data = fopen(filename, "r");
      if (fp_data == NULL) {
          fprintf(stderr, "Cannot open %s\n", filename);
          exit(EXIT_FAILURE);
      }
      while (fgets(new_line, MAX_LINE, fp_data) != NULL) {
          data_struct = (athlete_t *)malloc(sizeof(*data_struct));
          ini_struct(data_struct);
          insert_to_struct(data_struct, new_line);
          tree = insert_in_order(tree, data_struct);
          printf("%s\n", new_line);
      }
      printf("%s \n", new_line);
      fclose(fp_data);
      return tree;
  }
  
  /* initialize the struct */
  void ini_struct(athlete_t *data_struct) {
      strcpy(data_struct-&gt;data.id, "");
      strcpy(data_struct-&gt;name, "");
      strcpy(data_struct-&gt;data.sex, "");
      strcpy(data_struct-&gt;data.age, "");
      strcpy(data_struct-&gt;data.height, "");
      strcpy(data_struct-&gt;data.weight, "");
      strcpy(data_struct-&gt;data.team, "");
      strcpy(data_struct-&gt;data.noc, "");
      strcpy(data_struct-&gt;data.games, "");
      strcpy(data_struct-&gt;data.year, "");
      strcpy(data_struct-&gt;data.season, "");
      strcpy(data_struct-&gt;data.city, "");
      strcpy(data_struct-&gt;data.sport, "");
      strcpy(data_struct-&gt;data.event, "");
      strcpy(data_struct-&gt;data.medal, "");
  }
  
  /* make a empty tree */
  tree_t *make_empty_tree(int func(void *, void *)) {
      tree_t *tree;
      tree = (tree_t *)malloc(sizeof(*tree));
      assert(tree != NULL);
      tree-&gt;root = NULL;
      tree-&gt;cmp = func;
      return tree;
  }
  
  /* insert each value separated by comma into the struct we define */
  void insert_to_struct(athlete_t *data_struct, char *new_line) {
      sscanf(new_line, \
             "%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,]", \
             data_struct-&gt;data.id, data_struct-&gt;name, data_struct-&gt;data.sex, \
             data_struct-&gt;data.age, data_struct-&gt;data.height,\
             data_struct-&gt;data.weight,data_struct-&gt;data.team, \
             data_struct-&gt;data.noc, data_struct-&gt;data.games, \
             data_struct-&gt;data.year, data_struct-&gt;data.season, \
             data_struct-&gt;data.city, data_struct-&gt;data.sport, \
             data_struct-&gt;data.event, data_struct-&gt;data.medal);
  }
  
  int cmp(void *p1, void *p2) {
      char *s1 = (char *)p1;
      char *s2 = (char *)p2;
  
      return strcmp(s1, s2);
  }
  
  /* insert the struct into the tree */
  tree_t *insert_in_order(tree_t *tree, athlete_t *value) {
      node_t *new;
      new = malloc(sizeof(node_t));
      assert(new != NULL);
      new-&gt;data = value;
      new-&gt;left = new-&gt;right = NULL;
      tree-&gt;root = recursive_insert(tree-&gt;root, new, tree-&gt;cmp);
      return tree;
  }
  
  node_t *recursive_insert(node_t *root, node_t *new, int cmp(void*, void*)) {
      if (root == NULL) {
          return new;
      } else if (cmp(new-&gt;data-&gt;name, root-&gt;data-&gt;name) &lt;= 0) {
          root-&gt;left = recursive_insert(root-&gt;left, new, cmp);
      } else {
          root-&gt;right = recursive_insert(root-&gt;right, new, cmp);
      }
      return root;
  }
  
  void recursive_traverse(node_t *root, void action(void *)) {
      if (root != NULL) {
          recursive_traverse(root-&gt;left, action);
          action(root-&gt;data-&gt;name);
          recursive_traverse(root-&gt;right, action);
      }
  }
  
  void traverse_tree(tree_t *tree, void action(void *)) {
      assert(tree != NULL);
      recursive_traverse(tree-&gt;root, action);
  }
  
  void action(void *p1) {
      printf("%s\n", (char*)p1);
  }
  </code></pre>
  
  <p>The tricky thing happened in the <code>make_empty_tree</code> function, when I use <code>malloc</code> to create a pointer called new, but it doesn't work when I am debugging, but I think it should work!</p>