0

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.

1
  • 3
    char *malloc(); - what are you doing? Commented Feb 27, 2014 at 7:03

2 Answers 2

5

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) {
Sign up to request clarification or add additional context in comments.

3 Comments

i've already add #include <stdlib.h> but it still gives me same error
Remove the char *malloc()
To put a finer point on it: K&R function declarations have been obsolete since the release of the ANSI C standard in 1989. This was about 25 years ago. It's frankly amazing that compilers still tolerate it.
1
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>

2 Comments

The asker does not redefine malloc(), just declare it with a wrong type.
@leeduhem thanks for spotting that. updated the answer:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.