【发布时间】:2017-04-09 17:30:42
【问题描述】:
我需要在不使用“随机”函数的情况下生成一个随机数,所以我发现这段代码在系统中给了我毫秒:
#include <stdio.h>
#include <sys/timeb.h>
int main()
{
struct timeb tmb;
ftime(&tmb);
printf("tmb.time = %ld (seconds)\n", tmb.time);
printf("tmb.millitm = %d (mlliseconds)\n", tmb.millitm);
return 0;
}
在我尝试以这种方式在 struct 中使用它之前,它一直运行良好:
#include <sys/timeb.h>
#include "stdlib.h"
struct Num{
struct timeb tmb;
ftime(&tmp);
};
int main()
{
return 0;
}
它在 {ftime} 给出错误。 有什么帮助吗?
【问题讨论】:
-
“一个错误”?而实际的错误是?
-
啊,刚刚看到了:你为什么要从结构的声明中调用函数(即在函数之外)?因为……嗯……为什么会这样?
-
它的作业,我们需要在结构中生成一个随机数,然后在 main 中使用它(我不能使用 get_time 或随机函数)
-
嗯,这是 C,结构是数据类型,不包含代码。因此,如果提示是“在结构中执行此操作”,则答案是“语言无法执行此操作”。
-
提示是“不要在主函数中使用这个”