Thread: C: Errors for Incompatible Types

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    9

    Unhappy C: Errors for Incompatible Types

    I am writing a program in C. The following is an extract from my code:

    Code:
    enum coin_types
    {
        FIVE_CENTS=5,
        TEN_CENTS=10,
        TWENTY_CENTS=20,
        FIFTY_CENTS=50,
        ONE_DOLLAR=100,
        TWO_DOLLARS=200
    };
    
    struct coin
    {
        enum coin_types denomination;
        unsigned count;
    };
    
    
    void * safe_malloc(size_t size)
    {
        void * mem = malloc(size);
        if(!mem)
        {
            perror("Failed to allocate memory");
            abort();
        }
        return mem;
    }
    
    
    
    FILE * coinsf = NULL;
       char line[600];
    
    
       coinsf = fopen(coinsfile, "r");
       
       /* Read in each line, while line is not null */
       while(fgets(line, 600, coinsf) != NULL)
        {
           struct coin * new;
    
    
              line[strlen(line)-1] = 0;
          
           new = new_coins_data_line(line);
            }
    
    
    /* this function is in another file */
    struct coin new_coins_data_line(char * line)
    {
    
    
       struct coin * newdata = NULL;
       newdata = (struct coin *)
          safe_malloc(sizeof(struct coin));
       
       return newdata;
       
    }
    I'm getting the following errors:
    For: new = new_coins_data_line(line);
    "Incompatible types in assignment"

    For: return newdata;
    "Incompatible types in return"

    There seem to be problems with my variables and perhaps it is related to the type 'struct coin' which has an enumerated type within it.

    Please help me fix these errors. Thank you.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "struct coin" and "struct coin *" are two different types. Decide if you are returning a struct or a pointer to a struct.

    Code:
    struct coin new_coins_data_line(char * line)
    Tim S.
    Last edited by stahta01; 10-23-2013 at 08:36 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Additionally, you're not closing coinsf (or even checking that is was opened for that matter).

    As an aside, and not that it matters, let's also look at these lines (even though they're not the source of any error -- at the moment):

    Code:
    char line[600];
    //...
    while(fgets(line, 600, coinsf) != NULL)
    //...
    I don't particularly like that 600. You could do something like #define MAXLINELEN 600 and use that in both your array declaration and in the fgets, but is there something better you could replace the 600 in the fgets with?

    As food for thought, consider what the value of foo might be after the following line is executed:

    Code:
    size_t foo = sizeof line;
    and also
    Code:
    size_t foo = sizeof line / sizeof line[0];
    If you change the "element type" of line to, say, int line[600], what are the values of foo then?

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    9
    Quote Originally Posted by stahta01 View Post
    "struct coin" and "struct coin *" are two different types. Decide if you are returning a struct or a pointer to a struct.

    Code:
    struct coin new_coins_data_line(char * line)
    Tim S.

    Thanks, I had completely overlooked the * in the function.
    I have changed it to:
    struct coin * new_coins_data_line(char * line)
    and that error is gone now.

    I still have an error for:
    new = new_coins_data_line(line);
    saying "incompatible types in assignment"

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Do you know what a function prototype is?
    If yes, use one or correct the one you are using.


    Code:
    // Function Prototype must be before first use of function in the file.
    struct coin * new_coins_data_line(char * line);
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    9
    Quote Originally Posted by stahta01 View Post
    Do you know what a function prototype is?
    If yes, use one or correct the one you are using.


    Code:
    // Function Prototype must be before first use of function in the file.
    struct coin * new_coins_data_line(char * line);
    This fixed it! Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. incompatible types in assignment
    By tetartos in forum C Programming
    Replies: 9
    Last Post: 11-27-2011, 09:39 AM
  2. incompatible types in assignment
    By Adam Rinkleff in forum C Programming
    Replies: 2
    Last Post: 07-13-2011, 01:10 AM
  3. Incompatible types in assignment
    By Taka in forum C Programming
    Replies: 4
    Last Post: 10-17-2006, 05:33 AM
  4. incompatible pointer types
    By mart_man00 in forum C Programming
    Replies: 3
    Last Post: 04-20-2003, 08:32 PM
  5. incompatible types huh??
    By skanxalot in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2002, 09:05 PM