Incompatible types in assignment.

This is a discussion on Incompatible types in assignment. within the C Programming forums, part of the General Programming Boards category; Hi, i seem to be getting this error a lot, one of the main problems is i don't understand what ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    44

    Incompatible types in assignment.

    Hi, i seem to be getting this error a lot, one of the main problems is i don't understand what it really means. The code i was writing below today was just a linked list to store and print out a set of phone numbers.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct L
    {
    	char *number ;
    	struct L *next ;
    } list ;
    
    list insertnumber( char *NUMBER, list *n1 )
    {
    	list *n ; 
    	n = malloc( sizeof( list ) ) ;
    	n -> number = NUMBER ;
    	n -> next = n1 ;
    	return n ;
    	free(n) ;
    }
    
    void printl( list *n )
    {
    	while( n != NULL )
    	{
    		printf( "%s\n", n -> number ) ;
    		n = n -> next ;
    	}
    }
    
    int main( void )
    {
    	char num[15] ;
    	int i ;
    	list *n = NULL ;
    	
    	//num = malloc( sizeof( list ) ) ;
    
    	/*while( num != '.' )
    	{
    		for( i = 0 ; i < 15 ; i++ )
    			num[i] = getchar() ;
    		num = insertnumber( n, num ) ;
    	}*/
    
    	while( num[0] != '.' )
    	{
    		scanf( "%16s", num ) ;
    		n = insertnumber( num, n ) ;
    	}
    	printl( n ) ;
    	return 0 ;
    }
    I've tried it both times messing around with input. Currently it's using scanf with getchar commented out. Also should i try to be using getchar more than scanf?? I hear bad things about scanf. Right well anyway back to the point im getting these compile errors:

    nlist.c: In function `insertnumber':
    nlist.c:16: error: incompatible types in return
    nlist.c: In function `main':
    nlist.c:47: error: incompatible types in assignment

    Any feedback on these errors would be much appreciated. I'm brushing up on C over Christmas and have found my real weakness is debugging, so any feedback would be welcome.


    Cheers.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    insertnumber returns a list struct, but you are returning a pointer to a list struct and trying to assign a list to a pointer to list.
    Furthermore, look out in insertnumber. If you plan to return a pointer, you must not free it, since it would be invalid then. Moreover, you cannot or should not place any code after a return statement since it won't get executed.

    You should bump up your warnings.

    I also suggest you take a long hard look at what your code does. At the moment, it does not make sense.
    Use a flowchart if you must.
    And do not forget to free every node in the list before the program terminates.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    Bump up my warnings?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by killpoppop View Post
    Hi, i seem to be getting this error a lot, one of the main problems is i don't understand what it really means. The code i was writing below today was just a linked list to store and print out a set of phone numbers.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct L
    {
    	char *number ;
    	struct L *next ;
    } list ;
    
    list insertnumber( char *NUMBER, list *n1 )
    {
    	list *n ; 
    	n = malloc( sizeof( list ) ) ;
    	n -> number = NUMBER ;
    	n -> next = n1 ;
    	return n ;
    	free(n) ;
    }
    
    void printl( list *n )
    {
    	while( n != NULL )
    	{
    		printf( "%s\n", n -> number ) ;
    		n = n -> next ;
    	}
    }
    
    int main( void )
    {
    	char num[15] ;
    	int i ;
    	list *n = NULL ;
    	
    	//num = malloc( sizeof( list ) ) ;
    
    	/*while( num != '.' )
    	{
    		for( i = 0 ; i < 15 ; i++ )
    			num[i] = getchar() ;
    		num = insertnumber( n, num ) ;
    	}*/
    
    	while( num[0] != '.' )
    	{
    		scanf( "%16s", num ) ;
    		n = insertnumber( num, n ) ;
    	}
    	printl( n ) ;
    	return 0 ;
    }
    I've tried it both times messing around with input. Currently it's using scanf with getchar commented out. Also should i try to be using getchar more than scanf?? I hear bad things about scanf. Right well anyway back to the point im getting these compile errors:

    nlist.c: In function `insertnumber':
    nlist.c:16: error: incompatible types in return
    nlist.c: In function `main':
    nlist.c:47: error: incompatible types in assignment

    Any feedback on these errors would be much appreciated. I'm brushing up on C over Christmas and have found my real weakness is debugging, so any feedback would be welcome.


    Cheers.
    cannot free() what is in use so take that line in red out.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by killpoppop View Post
    Bump up my warnings?
    Every compiler typically has a setting that says how much warnings it should output. This option should always be maxed out - so that it gives as many warnings as possible.
    For example, in your code, you have unreachable code, as pointed out, and the compiler should warn about that.
    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.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    Have done im now returning:

    return *n ;

    which seems to remove the error.

    in main i've now changed it to:

    *n = insertnumber( num, n ) ;

    again it removes the error.

    These the correct steps to make?

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    Quote Originally Posted by Elysia View Post
    Every compiler typically has a setting that says how much warnings it should output. This option should always be maxed out - so that it gives as many warnings as possible.
    For example, in your code, you have unreachable code, as pointed out, and the compiler should warn about that.
    Ah i see that would be useful, i'll change my compiler options =] Thanks!

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    No, the first destroys the usefulness of a linked list.
    The second will cause a runtime error when you run it.
    You need to fully understand how pointers work and what they are good for.
    Here are some resources:
    http://cslibrary.stanford.edu/104/
    Memory Functions - Still Learning

    I would review them first and see if you can get a better understanding of how they work, and then see if you can understand what you do is wrong.

    Oh yes, and also
    http://cpwiki.sourceforge.net/Common...llocating_them
    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.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    yes those are the correct changes since insertnumber() returns an object of type list instead of a pointer to it.
    also remove that free(n) inside insertnumber(); shouldn't free what's in use esp. not after a return.

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    Haha who to believe?! =D But i am getting runtime errors. So obviously i'm wrong.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by itCbitC View Post
    yes those are the correct changes since insertnumber() returns an object of type list instead of a pointer to it.
    also remove that free(n) inside insertnumber(); shouldn't free what's in use esp. not after a return.
    No, it is not the correct changes or the solution. It will compile, yes, but it will not run and if fixed so that it does run, it still is not correct, since it defies the purpose of the linked list.
    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.

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    Yup your right, thanks for the help, i have some work to do =]

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Elysia View Post
    No, it is not the correct changes or the solution. It will compile, yes, but it will not run and if fixed so that it does run, it still is not correct, since it defies the purpose of the linked list.
    For the sake of my learning and pure knowledge PLMK how it defies the purpose of the linked list.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    I'm referring to the point that by returning a struct, a copy is made, which is not the original. Thus, if the original is actually modified, the copy is not. So you end up not with just a waste of execution time for copying the struct, not once, but possible twice, and end up with a copy of the list, as well.
    This may or may not defy the purpose of a list, but it certainly might not be a good idea. Especially since a list consists of linked nodes.
    If you had a pointer to it, then you would have no such problems.
    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.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Elysia View Post
    I'm referring to the point that by returning a struct, a copy is made, which is not the original. Thus, if the original is actually modified, the copy is not. So you end up not with just a waste of execution time for copying the struct, not once, but possible twice, and end up with a copy of the list, as well.
    There is no copy made of the list object as it is defined inside insertnumber() only.
    Quote Originally Posted by Elysia View Post
    This may or may not defy the purpose of a list, but it certainly might not be a good idea. Especially since a list consists of linked nodes.
    If you had a pointer to it, then you would have no such problems.
    The pointer to the list of nodes is contained within the structure itself so there is no deviation from the norm of linked lists.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assignment from incompatible pointer types
    By iunah in forum C Programming
    Replies: 13
    Last Post: 10-12-2008, 03:32 AM
  2. incompatible types in assignment
    By vapanchamukhi in forum C Programming
    Replies: 6
    Last Post: 09-19-2008, 07:45 AM
  3. incompatible types in assignment
    By vapanchamukhi in forum C Programming
    Replies: 1
    Last Post: 09-18-2008, 11:35 PM
  4. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  5. Incompatible types in assignment error
    By Zildjian in forum C Programming
    Replies: 12
    Last Post: 10-03-2003, 01:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21