正确使用malloc()

穆斯塔法

我在用C编写代码时使用malloc,但是我越来越

[警告]内置函数'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);
} 

提前致谢。

乔纳森·莱因哈特(Jonathon Reinhart)

您需要#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) {

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章