0

Hi i am trying to insert char *keyin = NULL and char *valuein = NULL; to my binary search tree, but error message appears like: Yelp1.c:111:11: error: assignment to expression with array type r->name = key; ^ Yelp1.c:112:11: error: assignment to expression with array type r->data = value; How should i modify this? Here is my 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 into 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 is not created created
    {
        r = (struct node*) malloc(sizeof(struct node)); // create a new node
        // insert data to new node
        r->name = key;
        r->data = value;
        // make left and right childs empty
        r->left = NULL;   
        r->right = NULL;
    }
    // if the data is less than node value then we must put this in left sub-tree
    else if(strcmp(key, r->name) < 0){ 
        r->left = insertion(r->left, key, value);
    }
    // else this will be in the 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;
}
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065

1 Answers1

1

The error message means you're trying to assign a pointer to an array:

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

r->name and r->data are arrays; you cannot assign directly to an array, but instead only to an element of the array.

Instead the direct assignment, try the following, as long as you are guaranteed that the length of key will always be less than MAXNAMELENGTH and the length of value will always be less than MAXDATALENGTH:

strcpy(r->name, key);
strcpy(r->data, value);
J Earls
  • 1,792
  • 8
  • 12