Thread: Confused about warning: return from incompatable pointer type

  1. #1
    Registered User
    Join Date
    Nov 2014
    Location
    Chicago, Illinois, United States
    Posts
    4

    Confused about warning: return from incompatable pointer type

    I am a bit confused as to why I am getting the warning "return from incompatable pointer type" with the code I have written below. I define yelp as a YelpDataBst* and the function has a return type of YelpDataBst*, so why am I getting this warning?

    Code:
    typedef struct BusList_t{
       long int addressOffset;
       long int reviewOffset;
       struct BusList_t* next;
    }BusList;
    
    
    typedef struct BusTree_t{
      char* name;
      //int id;
      BusList* locations;
      struct BusTree_t* left;
      struct BusTree_t* right;
    }BusTree;
    
    struct YelpDataBst{
      BusTree* Bushead;
    };
    
    struct YelpDataBST* create_business_bst(const char* businesses_path, const char* reviews_path){
      
      if(fopen(businesses_path,"r") == NULL || fopen(reviews_path,"r") == NULL)
        return NULL;
      FILE* fp_bp = fopen(businesses_path, "r");
      FILE* fp_rp = fopen(reviews_path, "r");
      struct YelpDataBst* yelp = malloc(sizeof(struct YelpDataBst));
      
      int ID = -1;
      int tempID;
      long int addressOffset;
      long int reviewOffset;
      char line[2000];
      char line2[2000];
      char* token;
      char* token2;
      char* name;
      int len;
      
      BusList* busListHead = NULL;
      BusTree* busTreeNode = NULL;
      BusTree* busTreeHead = NULL;
    
       ID = -1;
       tempID = 0;
       fgets(line,2000,fp_rp);
       fgets(line2,2000,fp_bp);
       fseek(fp_rp,0, SEEK_SET);
       fseek(fp_bp,0,SEEK_SET);
       int ct = 0;
       while(!feof(fp_rp)){
         
         len = strlen(line);
         token = strtok(line, "\t");
         tempID = atoi(token);
         if(ct == 0){
           tempID = 1;
           ct++;
         }
         if((ID != tempID || (ID < 0)) && tempID != 0){
        token2 = strtok(line2, "\t");
        addressOffset = ftell(fp_bp);
           if(tempID == 1)
          tempID = 0;
        token2 = strtok(NULL, "\t");
        if(token2 != NULL){
          if(name != NULL)
            if(strcmp(token2, name) == 0){
              fgets(line2, 2000,fp_bp);
              token2 = strtok(line2, "\t");
              token2 = strtok(NULL, "\t");
            }
          name = strdup(token2);
        }
        reviewOffset = ftell(fp_rp);
        if(tempID != 0)
          reviewOffset -= len;
        ID = atoi(token);
        busTreeNode = BusTree_create(name, busListHead, addressOffset, reviewOffset);
        busTreeHead = BusTree_insert(busTreeHead, busTreeNode); //replace with create node for tree
        free(name);
        fgets(line2,2000,fp_bp);
          }
          fgets(line,2000,fp_rp);
          
    }    
      yelp->Bushead = busTreeHead;
      BusTree_print(yelp->Bushead);
      BusTree_destroy(busTreeHead);
      return yelp;
      
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by Purdue View Post
    ...and the function has a return type of YelpDataBst*
    Umm...no it doesn't:
    Code:
    struct YelpDataBST* create_business_bst(const char* businesses_path, const char* reviews_path){
    C is case sensitive

  3. #3
    Registered User
    Join Date
    Nov 2014
    Location
    Chicago, Illinois, United States
    Posts
    4
    Oh gosh...I can't believe I didn't see that Thank you very much for pointing that out to me

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,112
    Code:
      if(fopen(businesses_path,"r") == NULL || fopen(reviews_path,"r") == NULL)
        return NULL;
      FILE* fp_bp = fopen(businesses_path, "r");
      FILE* fp_rp = fopen(reviews_path, "r");
    This leaks file handles.
    Open them once, then test for NULL.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Quote Originally Posted by Salem View Post
    Code:
      if(fopen(businesses_path,"r") == NULL || fopen(reviews_path,"r") == NULL)
        return NULL;
      FILE* fp_bp = fopen(businesses_path, "r");
      FILE* fp_rp = fopen(reviews_path, "r");
    This leaks file handles.
    Open them once, then test for NULL.
    Code:
    if ( (fp_bp = fopen(businesses_path, "r")) == NULL || (fp_rp = fopen(reviews_path, "r")) == NULL )
    {
        return NULL;
    }
    else
    ..

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,112
    Quote Originally Posted by Al3 View Post
    Code:
    if ( (fp_bp = fopen(businesses_path, "r")) == NULL || (fp_rp = fopen(reviews_path, "r")) == NULL )
    {
        return NULL;
    }
    else
    ..
    Try again - first file opens, the second one doesn't.
    Where do you close the first file?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Quote Originally Posted by Al3 View Post
    Code:
    if ( (fp_bp = fopen(businesses_path, "r")) == NULL || (fp_rp = fopen(reviews_path, "r")) == NULL )
    {
        return NULL;
    }
    else
    ..
    On my arch, FILE* and int are in the same size. You can do this too:

    Code:
    switch((int)fp)
    {
        case NULL : return NULL;
        break;
        default :
        // ...
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,849
    Instead of trying to combine the two fopen calls into a single if statement's condition, or using an unnecessary cast to int with a switch statement that looks like merely a fanciful if statement, I suggest replacing this:
    Code:
    if(fopen(businesses_path,"r") == NULL || fopen(reviews_path,"r") == NULL)
      return NULL;
    FILE* fp_bp = fopen(businesses_path, "r");
    FILE* fp_rp = fopen(reviews_path, "r");
    with:
    Code:
    FILE* fp_bp = fopen(businesses_path, "r");
    FILE* fp_rp = fopen(reviews_path, "r");
    if (!(fp_bp && fp_rp)) {
        fclose(fp_bp);
        fclose(fp_rp);
        return NULL;
    }
    or:
    Code:
    FILE* fp_bp = fopen(businesses_path, "r");
    if (!fp_bp) {
        return NULL;
    }
    FILE* fp_rp = fopen(reviews_path, "r");
    if (!fp_rp) {
        fclose(fp_bp);
        return NULL;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Quote Originally Posted by Salem View Post
    Try again - first file opens, the second one doesn't.
    Where do you close the first file?
    Umm.. It doesn't? I just created a pseudo-code for him how he can check and open in one expression.
    He will take care for the rest. ALSO.. when I was about to edit my next message to add one type-cast I forgot, the site crashed. I can see Database Error every day.
    So lets do a statistic.
    30% Of the times, I get misunderstood (My English is getting worse?)
    30% Of the times, the site crashes
    20% Of the times, I barely understand the question
    And there are 20% left, where I succeeded. Maybe I am used to s/o.
    Last edited by Al3; 11-25-2014 at 03:45 AM.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Code:
    FILE* fp_bp = fopen(businesses_path, "r");
    FILE* fp_rp = fopen(reviews_path, "r");
    if (!(fp_bp && fp_rp)) {
        fclose(fp_bp);
        fclose(fp_rp);
        return NULL;
    }
    O_o

    The use of `fclose(0)' relies on undefined behavior.

    [Edit]
    That issue may have been resolved in C11 standard.
    [/Edit]

    You should normalize this with `myfclose' or similar.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  11. #11
    Registered User Al3's Avatar
    Join Date
    Nov 2014
    Posts
    135
    Quote Originally Posted by phantomotap View Post
    O_o

    The use of `fclose(0)' relies on undefined behavior.

    [Edit]
    That issue may have been resolved in C11 standard.
    [/Edit]

    You should normalize this with `myfclose' or similar.

    Soma
    Hey pony, I call undefined behavior when someone, potentially suspicious ask a weird question.
    On the other hand..

    I've seen people to separate code with semicolons.
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    Code that makes sense:
    Code:
    int main(void) { return (0+1)-1; }
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    Race who will crash the exe with less code as
    possible. The winner's code:
    Code:
    int main(void) { main(); }
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    The worst attempt of C reinvention:
    Code:
    #define ULTIMATE_TERMINATOR(sexy) return sexy; }
    int main(void)
    {
        // do something as equally remarkable
        // ..
        ULTIMATE_TERMINATOR(0)
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    Code:
    #define NOTHING int main(void) { }
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    The most inoculate question:
    hiall im new 2 prograing and i need help
    can u help me or not if not i go
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    And on top of that "Windows" had to blow out with
    the following error when adjusting partition size:
    *Invalid Parameter*
    ...I mean what.. what is that even supposed to mean

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Al3 View Post
    Hey pony, I call undefined behavior when someone, potentially suspicious ask a weird question.
    In this context undefined behaviour means doing something which is not defined by the standard. For example, the standard defines what should happen if you call fclose(fp) and fp points to a valid file stream. But as far as I can tell the standard (at least versions C89 and C99) doesn't mention what should happen if you call fclose(0), where 0 does not point to a valid file stream. It's possible that you've tried this out on your gcc with glibc or whatever and noticed that it doesn't cause any harm. However if there's no standard saying what fclose(0) should do, then making any assumptions about its behaviour is what is meant by "relying on undefined behaviour".

  13. #13
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    in-before-grumpy

    O_o

    Yeah. There is a difference between undefined behavior and behavior not actually specified.

    *shrug*

    I just have a tendency to use them interchangeably.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,849
    Quote Originally Posted by phantomotap
    The use of `fclose(0)' relies on undefined behavior.
    Weird, I remember it as being well defined in C99, but no, it isn't. Wording seems to have been unchanged in the C11 late draft that I got my hands on some time ago.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by anduril462 View Post
    C is case sensitive
    Which is why it's a good idea to avoid identifiers and typenames that differ only by case, unless it ALL CAPS or all lowercase.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Warning: incompatible pointer type
    By Cnewbi in forum C Programming
    Replies: 7
    Last Post: 12-29-2011, 03:17 PM
  2. [Warning] Incompatible pointer type
    By dgs012 in forum C Programming
    Replies: 5
    Last Post: 02-20-2011, 11:27 AM
  3. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  4. Incompatible Pointer Type Warning
    By kwikness in forum C Programming
    Replies: 5
    Last Post: 10-30-2007, 06:14 PM

Tags for this Thread