首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

C语言链表输入元素时赋值有关问题

2012-04-22 
C语言链表输入元素时赋值问题这是我编写的一个小程序段,目的是:输入学号、姓名、性别,然后再输出。可是运行时

C语言链表输入元素时赋值问题
这是我编写的一个小程序段,目的是:输入学号、姓名、性别,然后再输出。
可是运行时第35、38和41行总有一个相同的错误提示:cpp(35) : error C2106: '=' : left operand must be l-value。
请问这是怎么回事啊?
具体程序如下:

C/C++ code
//目的:输入学号,姓名,性别.然后再输出#include <stdio.h>#include <malloc.h>struct goo {     char Student_number[10];    char name[20];     char sex[10];      float money;}; typedef struct node {     struct goo data;     struct node *link; }linklist,Node; void add(linklist*); void show(linklist*);void main(){    linklist *head;    head=(linklist*)malloc(sizeof(linklist));    head->link=NULL;    add(head);    show(head);}void add(linklist* head){    Node *q;    char sn[10];    char n[20],s[10];    q=(Node*)malloc(sizeof(Node));    printf("Please enter student number:");    scanf("%d",&sn);    q->data.Student_number=sn;   //有错,Why?    printf("Please enter name:");    scanf("%s",&n);    q->data.name=n;        //有错,Why?    printf("Please enter sex:");    scanf("%s",&s);        q->data.sex=s;          //有错,Why?    q->link=head->link;    head->link=q;}void show(linklist *head){    Node *p;    p=head->link;    while(p!=NULL)    {        printf("Student number:%d\n",p->data.Student_number);        printf("name:%s\n",p->data.name);        printf("sex:%s\n",p->data.sex);    }} 


[解决办法]
加#include<string.h>头文件,add函数改成下面这样:
C/C++ code
void add(linklist* head){    Node *q;    char sn[10];    char n[20],s[10];    q=(Node*)malloc(sizeof(Node));    printf("Please enter student number:");    scanf("%s", sn);  //要用%s吧,字符数组    strcpy(q->data.Student_number,sn);      printf("Please enter name:");    scanf("%s",n);    strcpy(q->data.name,n);            printf("Please enter sex:");    scanf("%s",s);        strcpy(q->data.sex, s);           q->link=head->link;    head->link=q;}
[解决办法]
C/C++ code
#include <stdio.h>#include <malloc.h>#include "string.h"  //添加头文件struct goo {     char Student_number[10];   //这里是char型    char name[20];     char sex[10];      float money;}; typedef struct node {     struct goo data;     struct node *link; }linklist,Node; void add(linklist*); void show(linklist*);void main(){    linklist *head;    head=(linklist*)malloc(sizeof(linklist));    head->link=NULL;    add(head);    show(head);}void add(linklist* head){    Node *q;    char sn[10];    char n[20],s[10];    q=(Node*)malloc(sizeof(Node));    printf("Please enter student number:");    scanf("%s",&sn);   //输入用%s,不是%d    strcpy(q->data.Student_number,sn);   字符串不能直接复制,用strcpy拷贝.    printf("Please enter name:");    scanf("%s",&n);    strcpy(q->data.name,n);         字符串不能直接复制,用strcpy拷贝.    printf("Please enter sex:");    scanf("%s",&s);        strcpy(q->data.sex,s);           字符串不能直接复制,用strcpy拷贝.    q->link=head->link;    head->link=q;}void show(linklist *head){    Node *p;    p=head->link;    while(p!=NULL)    {        printf("Student number:%s\n",p->data.Student_number);        printf("name:%s\n",p->data.name);        printf("sex:%s\n",p->data.sex);        p=p->link;   //指针后移,这个你在show的时候没有加.    }}
[解决办法]
fp=fopen("out.txt","w");
fprintf(fp,"%d\t%s\t%s\n",&(p->data.Student_num),p->data.name,p->data.sex);

热点排行