只是想把我的头绕在链表上。我收到一个错误“分配给类型 char[50] 时的类型不兼容”以及“字符串常量太长”最后一个结构中的类型参数“->”无效。我需要解决什么问题?
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
struct trainset {
char name[50];
int price;
struct trainset *next;
};
void show_list(struct trainset *list);
int main (void){
struct trainset *root;
root = (struct trainset *)malloc(sizeof(struct trainset));
strncpy(root ->name, " ", 50);
root ->price = 0;
root ->next = 0;
struct trainset *first_train;
first_train = (struct trainset *) malloc(sizeof(struct trainset));
root ->next = first_train;
strncpy(first_train->name, "Fantasy Train Set", 50);
first_train->price = 129;
first_train->next = NULL;
struct trainset *second_train;
second_train = (struct trainset *)malloc(sizeof(struct trainset));
first_train->next = second_train;
strncpy(first_train->name, "Uncle Bobs train set", 50);
second_train ->price = 69;
second_train ->next = NULL;
struct trainset *third_train;
third_train = (struct trainset *)malloc(sizeof(struct trainset));
second_train->next = third_train;
strncpy(third_train ->name, "Budha Bread Train", 50);
third_train ->price = 169;
third_train ->next = NULL;
show_list(first_train);
return 0;
}
void show_list(struct trainset *list)
{
while(list->next!=NULL)
{
printf("train set name: %s, Train price: %d\n", list->name, list->price);
list = list->next;
}
printf("train set name: %s, train price: %d", list->name, list->price);
}