gpt4 book ai didi

c - 正确使用 malloc()

转载 作者:太空宇宙 更新时间:2023-11-04 05:39:51 26 4
gpt4 key购买 nike

我在用 C 编写代码时使用 malloc,但我得到了

[Warning] conflicting types for built-in function 'malloc'

编译时出现这个警告。

这是我使用的代码:

starttimer(AorB,increment)
int AorB; /* A or B is trying to stop timer */
float increment;
{

struct event *q;
struct event *evptr;
char *malloc();

if (TRACE>2)
printf(" START TIMER: starting timer at %f\n",time);
/* be nice: check to see if timer is already started, if so, then warn */
/* for (q=evlist; q!=NULL && q->next!=NULL; q = q->next) */
for (q=evlist; q!=NULL ; q = q->next)
if ( (q->evtype==TIMER_INTERRUPT && q->eventity==AorB) ) {
printf("Warning: attempt to start a timer that is already started\n");
return;
}

/* create future event for when timer goes off */
evptr = (struct event *)malloc(sizeof(struct event));
evptr->evtime = time + increment;
evptr->evtype = TIMER_INTERRUPT;
evptr->eventity = AorB;
insertevent(evptr);
}

提前致谢。

最佳答案

您需要 #include <stdlib.h> ,并删除您的虚假声明:char *malloc();

此外,您需要找到更新的 C 引用!K&R 函数声明语法已经过时很长时间了。

考虑改变:

starttimer(AorB,increment)
int AorB; /* A or B is trying to stop timer */
float increment;
{

(看起来很正常的)ANSI C 标准:

int starttimer(int AorB, float increment) {

关于c - 正确使用 malloc(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22061286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com