结构体里面能否有float 谢谢

scfrank 2002-04-04 07:46:07
下面程序运行时被中断 说是浮点指针错误 把struct node 里面的 score 从float改
为long 程序便可正常运行 请问为什么?
#define NULL 0
#include <stdio.h>
#include <stdlib.h>

struct node
{long num;
float score;
struct node *next;
};



struct node *creat (void)
{struct node *head;
struct node *p1,*p2;
head=NULL;
p1=p2=(struct node *)malloc(sizeof(struct node));
clrscr();

printf ("input -1 to stop \n");
printf ("input the num :");
scanf ("%ld",&p1->num);
printf ("\ninput the score : ");
scanf ("%f",&p1->score);

while (p1->num!=-1)
{if (head==NULL) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct node *)malloc(sizeof(struct node));

clrscr();
printf ("input -1 to stop \n");
printf ("input the num :");
scanf ("%ld",&p1->num);
printf ("\ninput the score : ");
scanf ("%f",&p1->score);
}
return (head);
}

main()
{creat();};














...全文
194 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
scfrank 2002-04-10
  • 打赏
  • 举报
回复
在TC中把struct定义的变量(包括各个成员)作为一个存储单元,因此在scanf中用&p->coef,这是有问题的。
---------------------------------------这是我老师的回答.
1fei 2002-04-09
  • 打赏
  • 举报
回复
在VC里他说的问题一样存在
是scanf的问题
我就是在VC中测试的
onlynight 2002-04-07
  • 打赏
  • 举报
回复
你在vc里做 应该就没有问题
ukyoking 2002-04-06
  • 打赏
  • 举报
回复
我还是觉得是struct不允许直接使用float
charles_y 2002-04-06
  • 打赏
  • 举报
回复
是不是编译器有问题。
在头上加一句
#pragma pack(1)
看看行不行。
1fei 2002-04-06
  • 打赏
  • 举报
回复
应当是scan的问题
scfrank 2002-04-05
  • 打赏
  • 举报
回复
我莫衷一是了
但是ifei(白天)的方法我试了 好用

但是到底是不是sturct里面不能有float或者本身是scanf的一个bug 请指教
MSVCer 2002-04-05
  • 打赏
  • 举报
回复
VC没问题,我以前用过
Sword_Sharp 2002-04-05
  • 打赏
  • 举报
回复
*****************************************************************
如果你用的是tc那就有可能是软件本身的bug,有很多人都遇到过,你换vc式一下
*****************************************************************
cplusc 2002-04-05
  • 打赏
  • 举报
回复
你用什么编译器, 在Compile选项中有 struct align / record align吗?
cplusc 2002-04-05
  • 打赏
  • 举报
回复
c结构体中不允许使用float类型, 我第一次听说,
你提到 "如果交换 浮点和长整形的 输入顺序 程序也会不一样"

是不是结构字节对齐问题
1fei 2002-04-05
  • 打赏
  • 举报
回复
或者给结构赋初值也可
struct node *p1,*p2;
head=NULL;
p1=p2=(struct node *)malloc(sizeof(struct node));
p1->score =0;
clrscr();

printf ("input -1 to stop \n");
printf ("input the num :");
scanf ("%ld",&p1->num);
printf ("\ninput the score : ");
scanf ("%f",&p1->score);
1fei 2002-04-05
  • 打赏
  • 举报
回复
printf ("input -1 to stop \n");
printf ("input the num :");
scanf ("%ld",&p1->num);
printf ("\ninput the score : ");
scanf ("%f",&p1->score);

改为
printf ("input -1 to stop \n");
printf ("input the num :");
scanf ("%ld",&p1->num);
printf ("\ninput the score : ");
float d;
scanf ("%f",&d);
p1->score = d;
就可以了

xaocao 2002-04-05
  • 打赏
  • 举报
回复
您在问题我也遇过,我尝试过改变编译模式,不行!
在TC中结构体不能使用float!
ukyoking 2002-04-05
  • 打赏
  • 举报
回复
倒,居然还在讨论这个问题,让权威的我来告诉你们吧,结构体中不允许使用float类型,至少c中是这样,快给分^0^
scfrank 2002-04-05
  • 打赏
  • 举报
回复
也不会使程序说浮点指针错误啊
MSVCer 2002-04-05
  • 打赏
  • 举报
回复
hehe,程序中只有malloc,没有free
scfrank 2002-04-05
  • 打赏
  • 举报
回复
帖子次数大于给分次数是什么意思?
scfrank 2002-04-05
  • 打赏
  • 举报
回复
帖子次数大于给分次数是什么意思?
scfrank 2002-04-05
  • 打赏
  • 举报
回复
谢谢
如果是这样的话

可是为什么我把float改成long后 程序就没有错误呢
(刚听说别人如此也不能用)
谢谢 请指教

----------------------------------------------------
p2=(struct node *)malloc(sizeof(struct node));
p1=p2;
我试过也不行

----------------------------------------------------
而且在次程序中
如果交换 浮点和长整形的 输入顺序 程序也会不一样
------------------------------------------------
加载更多回复(6)

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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