开发者

Conflicting return types

I am doing a recursive program and I am getting an error about conflicting types:

开发者_JAVA技巧void* buddyMalloc(int req_size)
{ 
     // Do something here
     return buddy_findout(original_index,req_size); // This is the recursive call
}

void *buddy_findout(int current_index,int req_size)
{
    char *selected = NULL;

    if(front!=NULL)
    {
        if(current_index==original_index)
        {
            // Do something here
            return selected;
        }
        else
        {
            // Do Something here
            return buddy_findout(current_index+1,req_size);
        }
    }
    else
    {
        return buddy_findout(current_index-1,req_size);
    }
}

Error:

buddy.c: At top level:
buddy.c:76: error: conflicting types for ‘buddy_findout’
buddy.c:72: note: previous implicit declaration of ‘buddy_findout’ was here

Please note the file buddy.c in which I am defining this does not contain main and is linked with several other .c files.


You can't use a function prior to its proper definition without a prototype. buddy_malloc() is using buddy_findout() before it is prototyped or defined, which the compiler will treat as a definition.

Prototype buddy_findout() prior to defining buddy_Malloc(), or define buddy_findout() prior to defining buddy_Malloc().

I suggest prototypes, i.e:

void *buddy_Malloc(int);
void *buddy_findout(int, int);

... Just under your last #include

This avoids any confusion on the order that you define things. Also, consider using size_t (the largest unsigned int type available on the architecture) instead of signed integers when specifying sizes.

Here is your code (corrected) using both methods. Method 1 - using prototypes:

void *buddy_Malloc(int);
void *buddy_findout(int, int);

void* buddyMalloc(int req_size)
{ 
          //Do something here//
         return buddy_findout(original_index,req_size); //This is the recursive fn I call//
}

void *buddy_findout(int current_index,int req_size)
{
     char *selected = NULL;

     if(front!=NULL)
      {
            if(current_index==original_index)
            {
                    //Do something here//
                     return selected ; // 
             }
             else
             {
                 //Do Something here//
                 return buddy_findout(current_index+1,req_size);
              }
        }
       else
        {
                  return buddy_findout(current_index-1,req_size);
         }
}

And method 2, just re-ordering:

void *buddy_findout(int current_index,int req_size)
{
     char *selected = NULL;

     if(front!=NULL)
      {
            if(current_index==original_index)
            {
                    //Do something here//
                     return selected ; // 
             }
             else
             {
                 //Do Something here//
                 return buddy_findout(current_index+1,req_size);
              }
        }
       else
        {
                  return buddy_findout(current_index-1,req_size);
         }
}

void* buddyMalloc(int req_size)
{ 
          //Do something here//
         return buddy_findout(original_index,req_size); //This is the recursive fn I call//
}

In some circles it is sort of considered an art to not need prototypes for static functions, it kind of demonstrates the program was planned out in someone's head before code was written. I don't have much to say about that either way, other than recommending prototypes even for static functions to those who are still learning the nuts and bolts of C.

If buddy_* is going to be exposed for other modules to use, you really need prototypes. Its hard to tell if you intend these to be static or not.

Edit:

If you are putting the prototypes in an external header file, you need to use include guards to ensure that each module includes them only once (and doesn't re-define them to be the exact same thing).

Here is a sample buddy.h:

#ifndef BUDDY_H
#define BUDDY_H

void *buddy_Malloc(int);
void *buddy_findout(int, int);

#endif /* BUDDY_H */

The preprocessor will then keep your modules from throwing that error.


I guess this has to be with missing prototypes.

Add the function prototype of

void *buddy_findout(int current_index,int req_size);

before the function buddyMalloc


buddy_findout is declared to return a pointer to void, but in one place you're attempting to return selected, which is a pointer to char. As others have already pointed out, you also need a prototype for buddy_findout:

void *buddy_findout(int, int);

void *buddy_malloc(int req_size) { 
    return buddy_findout(original_index,req_size);
}

void *buddy_findout(int current_index, int req_size) {
// ...
    if (current_index == original_index)
        return (void *)selected;
// ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜