【发布时间】:2014-01-07 07:05:50
【问题描述】:
有人可以告诉我代码有什么问题吗?抱歉,如果我是新手,但我很长一段时间都在努力解决这个问题。我正在尝试从用户那里获取输入,然后打印这些值。下面的代码只是一个例子。
我尝试运行代码,但出现运行时错误。有人可以帮忙吗?
#include <stdio.h>
#include <stdlib.h>
typedef struct poly
{
int kill;
float bill;
char katana[50];
} koly;
typedef koly* terma;
int main()
{
int count = 0;
terma ren;
ren = (terma)malloc(sizeof(koly));
ren = (terma)realloc(6*sizeof(koly));
printf("We can store now:\n\n");
while(++count<= 2)
{
scanf("%d",ren->kill);
scanf("%f",ren->bill);
scanf("%s",ren->katana);
}
while(++count<= 2)
{
printf("\n%d\n",ren->kill);
printf("\n%f\n",ren->bill);
printf("\n%s\n",ren->katana);
}
}
【问题讨论】:
-
什么运行时错误?
-
realloc : 两个参数
-
各位大神,投出
malloc/realloc的返回值的撒旦做法变得更糟了。malloc& co 返回void *,一个指针,你正在投射它。 不要,尤其是使用您的typedef,这会掩盖您实际上在做什么 -
不确定为什么要重新分配?看来您只阅读 2 条记录 (` ++count
标签: c malloc scanf dynamic-memory-allocation realloc