mustafa
mustafa

Reputation: 109

Using malloc() properly

I use malloc while i was writing code in C, but i am getting

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

this warning when i compile.

and this is the code that i use :

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);
} 

thanks in advance.

Upvotes: 0

Views: 164

Answers (2)

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137398

You need to #include <stdlib.h>, and remove your bogus declaration: char *malloc();

Also, you need to find a newer C reference! The K&R function declaration syntax has been obsolete for a very long time.

Consider changing:

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

to the (sane-looking) ANSI C standard:

int starttimer(int AorB, float increment) {

Upvotes: 5

Sakthi Kumar
Sakthi Kumar

Reputation: 3045

char *malloc();

I am not sure why you redeclare the library function malloc. Any how here is the declaration of malloc

void *malloc(size_t size);

Please include <stdlib.h>

Upvotes: 1

Related Questions