I need to assign a string (char of size [255]) to an element in a struct. The struct looks like this:
struct node{
int ID, YEAR, MONTH, DAY
char CATEGORY[255], DETAIL[255];
double AMOUNT;
struct node * next;
}
struct node * head = NULL;
and I have code that gets values from a text file and sets it as a variable that I then pass to the add_struct function which looks like this:
void add_struct(int i, char c, char d, double a, int y, int m, int da){
if (head == NULL){
head = (struct node *) malloc(sizeof(struct node));
head->ID = i;
head->CATEGORY = c;
head->DETAIL = d;
head->AMOUNT = a;
head->YEAR = y;
head->MONTH = m;
head->DAY = da;
}
else {
struct node * p = head;
while(p->next != NULL) p = p->next;
p->next = (struct node *) malloc(sizeof(struct node));
p->next->ID = i;
p->next->CATEGORY = c;
p->next->DETAIL = d;
p->next->AMOUNT = a;
p->next->YEAR = y;
p->next->MONTH = m;
p->next->DAY = da;
}
}
I get error message:
"incompatible types when assigning to type 'char[255]' from type 'char'"
How do I assign these values to elements CATEGORY and DETAIL properly?
char c-->char *c...p->next->CATEGORY = c;-->strcpy(p->next->CATEGORY, c);char c, char d,really meant a single char or a pointer to char(string)?->next = NULL;malloc's return value, just dohead = malloc(sizeof(struct node));.