在写代码的时候出现这样的问题 不知道哪里错了

血墨 2017-10-15 03:16:16
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
typedef struct student
{
char name[1];
float score[1];
struct student *next;
}STU;
STU *myCreatelist(STU *head)
{
STU *q,*p;
char temp[10];
int flag=1;
float t=0;
head=(STU*)malloc(sizeof(STU));
head->next=NULL;
q=head;
while(flag)
{
p=(STU*)malloc(sizeof (STU));
p->next=NULL;
printf("input name\n");
scanf("%s",temp);
printf("input score\n");
strcpy(p->name,temp);
scanf("%f",&t);
p->score[0]=t;
q->next=p;
q=p;
printf("inpuut flag\n");
scanf("%d",&flag);
}
p=head->next;
while(p)
{
printf("name is :%s\n",p->name);
printf("score is :%.2f\n",p->score[0]);
p=p->next;
}
printf("\n");
return head;
}
void main ()
{
STU *head,*p,*q;
head=myCreatelist(head);
q=head->next;
while (q=q->next)
{
if(!strcmp (q->next->name,"x6"))
{
p=q->next;
q->next=p;
break;
}
}
p=head;
while (p=p->next)
{
printf("name is :%s\n",p->name);
printf("score is :%f\n",p->score[0]);
p=p->next;
}
}
...全文
179 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_33655727 2017-10-16
  • 打赏
  • 举报
回复
定义指针的时候顺带赋值NULL。
自信男孩 2017-10-16
  • 打赏
  • 举报
回复
# include <stdio.h>
# include <stdlib.h>
# include <string.h>

typedef struct student
{
    char name[32];
    float score;
    struct student *next;
}STU;

STU *myCreatelist(STU *head)
{
    STU *q,*p;
    char temp[10];
    int flag=1;
    float t=0;
    head=(STU*)malloc(sizeof(STU));
    head->next = NULL;
    q=head;
    while(flag)
    {
        p = (STU*)malloc(sizeof (STU));
        p->next = NULL;
        printf("input name\n");
        scanf("%s", temp);
        printf("input score\n");
        strcpy(p->name,temp);
        scanf("%f", &t);
        p->score = t;
        q->next=p;
        q = p;
        printf("inpuut flag\n");
        scanf("%d",&flag);
    }
    p=head->next;
    while(p)
    {
        printf("name is :%s\n",p->name);
        printf("score is :%.2f\n",p->score);
        p=p->next;
    }
    printf("\n");
    return head;
}
int main ()
{
    STU *head = NULL,*p,*q;
    head = myCreatelist(head);
    q = head->next;
    while (q)
    {
        if(!strcmp (q->name,"x6")) {
            p=q->next;
            q->next=p;
            break;
        } else {
            q = q->next;
        }
    }
    p = head->next;
    while (p)
    {
        printf("name is :%s\n",p->name);
        printf("score is :%f\n",p->score);
        p = p->next;
    }

    return 0;
}
结构体定义有问题,name定义的长度太短,只要输入一个字符就会导致溢出,score若是一个成绩,建议定义成变量而非数组。
棉猴 2017-10-16
  • 打赏
  • 举报
回复
《C++Primer第四版中文版》中提到 除了使用数值 0 或在编译时值为 0 的 const 量外,还可以使用 C++ 语言从 C 语言中继承下来的预处理器变量 NULL,该变量在 cstdlib头文件中定义,其值为 0。如果在代码中使用了这个预处理器变量,则编译时会自动被数值 0 替换。因此,把指针初始化为 NULL 等效于初始化为 0 值:
// cstdlib #defines NULL to 0
STU *head = NULL; // ok: equivalent to int *pi = 0;
血墨 2017-10-15
  • 打赏
  • 举报
回复
引用 1 楼 赵4老师的回复:
局部变量head没赋初值就用
可是指针如何附初值,设为全局变量吗
赵4老师 2017-10-15
  • 打赏
  • 举报
回复
局部变量head没赋初值就用

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧