Insert char * into binary search tree in C -


this question has answer here:

hi trying insert char *keyin = null , char *valuein = null; binary search tree, error message appears like: yelp1.c:111:11: error: assignment expression array type r->name = key; ^ yelp1.c:112:11: error: assignment expression array type r->data = value; how should modify this? here part of code:

#include <stdio.h> #include <stdlib.h> #include <string.h>   #define maxnamelenth 64 #define maxdatalenth 1465  typedef struct node {     char name[maxnamelenth];     char data[maxdatalenth];     struct node* left;     struct node* right; }node;  node* root; node* search(node ** tree, char *key, int count, file *fp_out); node* insertion(node* r, char *key, char *value); void deltree(node * tree); char* strtok_r(char *str, const char *delim, char **nextp);   int main(int argc, char *argv[]) {     node *root;     node *tmp;     file *fp;     file *outputfile;     file *keyfile;     file *fp_key;     int i;     int counter = 0;     int bufsize = maxdatalenth + maxnamelenth;     int keyfilelen = maxnamelenth;     char *keyread;     char *buffer,*saveptr;     char *line = null;     char *keyin = null;     char *valuein = null;     char inputkey[maxnamelenth];     char *target_key = null;     char *keyline = null;       root = null;      /* inserting nodes tree */     buffer = (char *)malloc(bufsize * sizeof(char));     if (buffer == null)     {         exit(1);     }      fp = fopen(argv[1], "r");     outputfile = fopen("outputfile.txt", "a");        while (1)     {         fgets(line, bufsize, fp);         buffer = line;         keyin = strtok_r(buffer, ",", &saveptr);         valuein = strtok_r(null, "\0", &saveptr);         insertion(root, keyin, valuein);     } node* insertion(node* r, char *key, char *value) {     if(r==null) // bst not created created     {         r = (struct node*) malloc(sizeof(struct node)); // create new node         // insert data new node         r->name = key;         r->data = value;         // make left , right childs empty         r->left = null;            r->right = null;     }     // if data less node value must put in left sub-tree     else if(strcmp(key, r->name) < 0){          r->left = insertion(r->left, key, value);     }     // else in right subtree     else if (strcmp(key, r -> name) > 0){          r->right = insertion(r->right, key, value);     }     else {         if(strcmp(value, r -> data) > 0){             r -> left = insertion(r -> left, key, value);          }         else if(strcmp(value, r->data) < 0){              r -> right = insertion(r->right, key, value);         }        }     return r; } 

the error message means you're trying assign pointer array:

r->name = key; r->data = value; 

r->name , r->data arrays; cannot assign directly array, instead element of array.

instead direct assignment, try following, long guaranteed length of key less maxnamelength , length of value less maxdatalength:

strcpy(r->name, key); strcpy(r->data, value); 

Comments