Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't store a string of characters in a node of linked list

Tags:

c

linked-list

I'm doing my coursework regarding airport simulation and I'm having some troubles trying to store information in the character array part.

I am supposed to type in a string of character and it will store in the planeName part of the node but it can't seem to work. My int main() is pretty much empty now because I didn't want to continue coding with incorrect functions.

Below are my codes:

struct node {
    char planeName[5];
    int planeNumber;
    struct node* next;
}; 

struct node* front = NULL;
struct node* rear = NULL;

void Enqueue(char name[5], int x);

int main() {

}

void Enqueue(char name[5], int x){

    struct node* temp = (struct node*)malloc(sizeof(struct node));

    temp -> planeName = name; 
    temp -> planeNumber = x;
    temp -> next = NULL;

    if (front == NULL && rear == NULL)
        front = rear = temp;
    rear -> next = temp; //set address of rear to address of temp
    rear = temp; //set rear to point to temp

    return;
}

This is the error message in the line containing: temp -> planeName = name

This is the part where error message pops up and I have no clue why is this happening.

Can someone please help and ask more questions me if my question is not clear enough?

like image 405
Kate Lee Avatar asked Feb 08 '23 04:02

Kate Lee


1 Answers

temp -> planeName = name;

You can't assign to an array. Array cannot be used as lvalue. Use strcpy instead-

strcpy(temp -> planeName,name);

Note- But make sure your char arrays are nul terminated before passing them to strcpy.

like image 88
ameyCU Avatar answered Feb 28 '23 05:02

ameyCU