Thread: assignment from incompatible pointer types

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    25

    assignment from incompatible pointer types

    i'm getting this error... i don't know what to do.. please help...

    Code:
    typedef struct node {
    	int age;
    	struct node *prev;
    	struct node *next;	
    } person;
    
    typedef struct node2 {
    	char name[10];
    	struct node2 *move;
    } person2;
    
    void move(person **a){
    //some code...
            person2 *p;
    	p = (person2 *)malloc(sizeof(person2));
    	p->move = *a; //this is where the error points at
    //some code...
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Person and person2 aren't the same type!
    You're trying to assign a person (aka node) to a pointer to node2 (aka person2).
    And you realize that it's possible to write person** a and person2* p as well?
    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
    Sep 2008
    Posts
    25
    thanks, but, can you please discuss more on solving this problem? i'm still new to C.. sorry..

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, if someone is expecting an orange, you don't hand them an apple, do you? Likewise, in both C and C++, the left and right side types of an assignment should match.
    You must understand that in these low-level languages, the compiler only knows the type by looking at your code. If you assign a person2 where a person is expected, you're going to get problems later (because the compiler will think it's a person2 while it's a person in reality).
    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.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    do I have to typecast one of the sides?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, typecasts do not change the underlying type when you cast pointers.
    Actually, I see you have two problems...
    You try to assign a non-pointer to pointer and try to assign incompatible types.
    I strongly suggest you go back to tutorials or books and re-read pointers and whatnot.
    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.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > p->move = *a;
    OK, so you cast it, make the warning go away and forget it.

    Then what happens next?

    Sometime later in the code, you're looking at a ptr->move
    Are you expecting to find at the other end of it a person or a person2?
    If you're expecting to find a person, then declare move as
    struct node *move;

    Otherwise, think carefully about what it is you're trying to do.
    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.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    but, what I want to do is to traverse person2 on person...

    what should I do?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Look, this isn't rocket science. Currently, you tell the compiler that person should contain nodes of persons. What do you have to do to change it to person2s?
    Do you know how to make flowcharts? Or pseudo code? I suggest you learn them, because it's obvious it's going to benefit you greatly.
    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.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    I think I got it.. thanks for all the help

    Code:
    typedef struct node2 {
    	char name[10];
    	struct node *move; //i removed '2' from node
    } person2;

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are you sure? Have you tried compiling?
    You still don't grasp pointers correctly.
    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
    Sep 2008
    Posts
    25
    Yes.. and it worked perfectly..

    Well it's just a part of a HUGE code I'm currently working on..

    Again, thanks for all the help..

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm skeptical, and you are probably causing memory leaks here and there, too.
    I think you need to scale down and study pointers more.
    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.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I'm busily trying to banish this thread from my memory, in the interests of sleeping tonight. <shudder>.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers, structures, and malloc
    By lugnut in forum C Programming
    Replies: 24
    Last Post: 10-09-2008, 04:52 PM
  2. incompatible types in assignment
    By vapanchamukhi in forum C Programming
    Replies: 6
    Last Post: 09-19-2008, 07:45 AM
  3. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  4. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  5. 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