#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Incompatible pointer types - linked list struct inside another struct


    First, my linked list:

    Code:
    struct sr_tt { 
        struct sr_tt * next; 
        ... 
    };
    Now, due to reasons I wont disclose I've been shoving what I assume is an instance of the above in another struct:

    Code:
    struct sr_inst { 
        struct sr_tt * my_tt; 
        ... 
    };
    What happens next is in my code I have this permanent instance of sr_inst (let's call it 's') being passed around everywhere, so I would do the following to get at my_tt:

    Code:
    struct sr_tt * curr = s->my_tt;
    ...
    curr = curr->next
    ...
    Now there were no complaints from the compiler about the first line. Declaration went along just fine, apparently. But I keep getting the following warning:

    "warning: assignment from incompatible pointer type" on the line that should, by all means, just let me iterate through my linked list.

    Why is it doing this and how do I make it stop that.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    Is struct sr_tt an in-scope declaration when it gets to struct sr_inst ?
    That is, in the same source file, or the same header file.

    It it isn't, then the struct sr_tt in sr_inst will be distinct from your actual struct st_tt.

    Can you produce a short example which fails to compile?
    Because I can't see how it fails at the moment.
    Code:
    $ cat bar.c
    #include <stdio.h>
    #include <stdlib.h>
    
    struct sr_tt { 
        struct sr_tt * next; 
    };
    
    struct sr_inst { 
        struct sr_tt * my_tt; 
    };
    
    struct sr_inst *s;
    
    int main()
    {
        struct sr_tt * curr = s->my_tt;
        curr = curr->next;
        return 0;
    }
    $ gcc -Wall bar.c
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by salem
    Is struct sr_tt an in-scope declaration when it gets to struct sr_inst ?
    That is, in the same source file, or the same header file.

    It it isn't, then the struct sr_tt in sr_inst will be distinct from your actual struct st_tt.

    Can you produce a short example which fails to compile?
    Because I can't see how it fails at the moment.
    Code:
    $ cat bar.c
    #include <stdio.h>
    #include <stdlib.h>
    
    struct sr_tt { 
        struct sr_tt * next; 
    };
    
    struct sr_inst { 
        struct sr_tt * my_tt; 
    };
    
    struct sr_inst *s;
    
    int main()
    {
        struct sr_tt * curr = s->my_tt;
        curr = curr->next;
        return 0;
    }
    $ gcc -Wall bar.c
    They are indeed in the same header file, sr_tt is defined right above sr_inst.

    I don't know if I can produce an example per-say, because this is basically an enormous pile of code and I'm not sure I'm allowed to post any of it...

    in more detail: I'm passing struct sr_inst* s into a function and then doing exactly this:

    Code:
    struct sr_tt * curr = s->my_tt;
        curr = curr->next;
    and getting that warning.

    If you can't make head or tails of what's going on, that's ok and thank you very much for trying. I'm sorry I can't provide any more information.

    I should note it compiles just fine. I just keep getting that warning.
    Last edited by MuchJokes; October 21st, 2012 at 12:22 AM. Reason: clicked reply early
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    > I don't know if I can produce an example per-say, because this is basically an enormous pile of code and I'm not sure I'm allowed to post any of it...
    Create a copy of the project and start hacking away at it to reduce it's size.

    An empty main(), and delete every other function except the one in question would be a good start.

    Another thing to try is just examine what the code looks like after it's been through the C pre-processor. This checks to see if anyone has been doing stupid things with #defines.
    Code:
    $ cat bar.c
    #include <stdio.h>
    #include <stdlib.h>
    
    struct sr_tt { 
        struct sr_tt * next; 
    };
    
    //!! WTF is this?
    #define sr_tt   foo
    
    struct sr_inst { 
        struct sr_tt * my_tt; 
    };
    
    struct sr_inst *s;
    
    int main()
    {
        struct sr_tt * curr = s->my_tt;
        curr = curr->next;
        return 0;
    }
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:20:16: error: dereferencing pointer to incomplete type
    $ gcc -Wall -E -o bar.i bar.c
    $ tail -20 bar.i
    
    struct sr_tt {
        struct sr_tt * next;
    };
    
    
    
    
    struct sr_inst {
        struct foo * my_tt;
    };
    
    struct sr_inst *s;
    
    int main()
    {
        struct foo * curr = s->my_tt;
        curr = curr->next;
        return 0;
    }
    As you can see here, what you thought was struct sr_tt has "not so mysteriously" changed into struct foo.
    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. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    UPDATE:

    I am the worlds biggest idiot.

    I accidentally wrote

    struct st_tt * next;

    and even after staring at it for bloody hours I only now noticed that.

    Thank you so much for the help, though! Sorry for wasting you time like that D:

IMN logo majestic logo threadwatch logo seochat tools logo