Thread: Incompatible Pointer Type Errors

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    4

    Incompatible Pointer Type Errors

    Hey I have been making an auction kind of prgram where the lowest unique bid wins. Upon compilation I ran into these errors that I could not fix:

    Auctions.c: In function ‘main’:
    Auctions.c:27: error: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
    Auctions.c: In function ‘insertNewBid’:
    Auctions.c:60: error: passing argument 1 of ‘insertNewBid’ from incompatible pointer type
    Auctions.c:62: error: passing argument 1 of ‘insertNewBid’ from incompatible pointer type

    Here is the code, any help/tips are appreciated:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct bids *Bids;
    
    #define TRUE 1
    #define FALSE 0
    
    struct bids {
    int bidAmount;
    int unique;
    struct Bids *right;
    struct Bids *left;
    };
    
    int findLowestUnique (Bids Auction);
    Bids newBid(int bid);
    void insertNewBid(Bids Auction, Bids newBids);
    
    int main (void) {
    
        int bid;
        Bids newBids = malloc(sizeof(Bids));
        Bids Auction = malloc(sizeof(Bids));
    
       // while(getchar() != NULL) {
            scanf("%d", &bid);
            newBids = newBid(bid);
            insertNewBid(Auction, newBids);
            printf("Bid of %d: ", bid);
            if (newBids->unique == FALSE) {
                printf("not unique ");
            } else if (findLowestUnique(Auction) == bid) {
                printf("new lowest unique bid! ");
            } else {
                printf("unique but not lowest ");
            }
            printf("[best bid: %d]", findLowestUnique(Auction));
        //}
    
        return(0);
    }
    
    Bids newBid(int bid) {
    
        Bids bids = malloc(sizeof(Bids));
        bids->bidAmount = bid;
        bids->unique = TRUE;
        bids->left = NULL;
        bids->right = NULL;
    
        return (bids);
    }
    
    void insertNewBid(Bids Auction, Bids newBids) {
        
        if (Auction == NULL) {
            Auction = newBids;
        } else if (newBids->bidAmount < Auction->bidAmount) {
            insertNewBid(Auction->left, newBids);
        } else if (newBids->bidAmount > Auction->bidAmount) {
            insertNewBid(Auction->right, newBids);
        } else if (newBids->bidAmount == Auction->bidAmount) {
            newBids->unique = FALSE;
            Auction->unique = FALSE;
        }
    
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >typedef struct bids *Bids;
    This is your problem. If you hide a pointer behind a typedef, you should be careful not to accidentally double the pointer like you did here:
    Code:
    struct bids {
    int bidAmount;
    int unique;
    struct Bids *right; /* struct bids **right; */
    struct Bids *left;  /* struct bids **left; */
    };
    Your incompatible pointer errors stem from that. My recommendation is not to hide levels of indirection in the first place.

    >error: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
    It looks like your compiler is pedantic about checking return values. Might I suggest wrapping your scanf in an if statement?
    Code:
    if ( scanf ( "%d", &bid ) != 1 ) {
      /* Handle the error */
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    4
    Thanks for the help, but it still doesn't seem to work. The scanf fix worked fine, but I changed the code to what you suggested:

    Code:
    struct bids {
    int bidAmount;
    int unique;
    struct bids **right;
    struct bids **left; 
    };
    but the error still happens. I understand there is still a problem with it being a pointer or a pointer to a pointer or something but just can't work out how to recursivley call insertNewBid. I have tried a few alternative ways... all to no avail, so any clues on how to do this would be very helpful.

    Thanks again

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I changed the code to what you suggested:
    No, you changed your code to the same damn thing. I strongly recommend you ditch the typedef and use pointers directly instead of hiding them, because that's the root of your problem. But I know you won't, so this is what your structure definition should be like:
    Code:
    struct bids {
      int bidAmount;
      int unique;
      struct bids *right;
      struct bids *left; 
    };
    When you say Bids x, it becomes struct bids *x. You were declaring right and left as Bids *x, which becomes struct bids **x. This is an extra level of indirection that causes your error. You "fixed" the code to exactly the same thing, and it should come as no surprise that the error persists.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    4
    Ohh ok I am starting to understand it better now. I would not usually use these typedef stuff but its for uni and they are just making us use them. I knew I was close but just didn't know enough about the theory behind them to pinpoint the error.

    Thanks for all the help

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can use a typedef as an alternate name for the struct, but never use it with a pointer-type. Eg:
    struct mystruct;
    typedef struct mystruct a_nice_name; /* Good */
    typedef struct mystruct* another_nice_name; /* Bad! */
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. incompatible pointer type?
    By kezman in forum C Programming
    Replies: 3
    Last Post: 04-22-2009, 04:42 PM
  2. Replies: 2
    Last Post: 04-11-2008, 02:42 AM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM
  5. problem with incompatible pointer type
    By SIKCAR in forum C Programming
    Replies: 7
    Last Post: 09-14-2002, 04:42 PM