我试图了解链接列表是如何工作的;在更改导师的示例代码时,我得到“左操作数必须是左值”错误......这是我的代码。任何帮助表示赞赏:)
struct node
{
char name[20];
struct node *next;
};
/*Function to allocate memory and initialize node - returns pointer to node*/
struct node*mknode(char *str)
{
struct node *np;
np = (struct node*)malloc(sizeof(struct node));
if(np)
{
np->name = *str;
np->next = NULL; /*sets the 'next' pointer to last list item to NULL*/
}
return np;
}
int main(void)
{
int i,menu,k=1,number;
char name[20];
char *str = name;
struct node* n;
struct node *head=NULL;
printf("Link Lists\n 1-Enter Data into lists\n 2-Display All List Items\n 3-Quit Program :(\n");
do
{
printf("\nPlease choose an option: ");
scanf("%d",&menu);
switch (menu)
{
case 1:
printf("\nPlease Enter A Number:");
scanf("%d",&name);
n=mknode(str); /*create new node with i as data*/
append_node(&head,n); /*add new node to end of list*/
break;
这是我得到错误的地方
np->name = *str;
将结构数据类型从整数更改为字符后
谢谢