The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Incompatible pointer types - linked list struct inside another struct
Discuss Incompatible pointer types - linked list struct inside another struct in the C Programming forum on Dev Shed. Incompatible pointer types - linked list struct inside another struct C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 20th, 2012, 11:42 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 3
Time spent in forums: 1 h 18 m 50 sec
Reputation 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.
|

October 21st, 2012, 12:08 AM
|
 |
Contributed User
|
|
|
|
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
|

October 21st, 2012, 12:17 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 3
Time spent in forums: 1 h 18 m 50 sec
Reputation Power: 0
|
|
Quote: | 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
|

October 21st, 2012, 12:43 AM
|
 |
Contributed User
|
|
|
|
> 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.
|

October 21st, 2012, 12:47 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 3
Time spent in forums: 1 h 18 m 50 sec
Reputation 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:
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|