Pointers in structures II (newbie question)

T

Theo Stauffer

Hi all, this is my first (utter C newbie) post, so I beg your patience.

I have a little code snippet:

typedef struct listtype {
struct list *list_ptr;
}list;

list l1,l2;


int main ()

{


l1.list_ptr = &l2;

}

When I try to compile this under Mac OSX, I get a compiler warning that
I'm assigning an incompatible pointer type. The strange and confusing
thing is that, in the 2 C tutorials I have they have conflicting
instructions on how to do this. The one says that it's fine, the other
doesn't.

However, when I change this to:

typedef struct listtype {
struct listtype *list_ptr;
}list;

list l1,l2;


int main ()

{


l1.list_ptr = &l2;

}

then it's happy and compiles. Is this a gcc version thing or am I (most
probably) just completely misunderstanding something? Be grateful for an
answer.

-Theo Stauffer
 
M

Mark A. Odell

Hi all, this is my first (utter C newbie) post, so I beg your patience.

I have a little code snippet:

typedef struct listtype {
struct list *list_ptr;

Nay, nay! The struct tag is 'listtype', not 'list' so this is an obvious
error. Ether stop using typdef's for this sort of thing and stick to
struct <tag> or make the typedef name the same as the struct tag name (you
can do this since typedefs are in a different name space). E.g. Either:

struct List
{
struct List *pList;
};

struct List list[2];

list[0].pList = &list[1];

---OR---

typedef struct List
{
struct List *pList;
} List;

List list[2];

list[0].pList = &list[1];
 
T

Theo Stauffer

Mark said:
Hi all, this is my first (utter C newbie) post, so I beg your patience.

I have a little code snippet:

typedef struct listtype {
struct list *list_ptr;


Nay, nay! The struct tag is 'listtype', not 'list' so this is an obvious
error. Ether stop using typdef's for this sort of thing and stick to
struct <tag> or make the typedef name the same as the struct tag name (you
can do this since typedefs are in a different name space). E.g. Either:

struct List
{
struct List *pList;
};

struct List list[2];

list[0].pList = &list[1];

---OR---

typedef struct List
{
struct List *pList;
} List;

List list[2];

list[0].pList = &list[1];
Thanks a million for that answer. I like C since it reminds me of the
Pascal that I learned about twenty years ago, but I get hellishly
confused sometimes about what is correct and what is not. Time to get a
K&R book I suppose, for the full specs of the language?
 
M

Mark A. Odell

Thanks a million for that answer. I like C since it reminds me of the
Pascal that I learned about twenty years ago, but I get hellishly
confused sometimes about what is correct and what is not. Time to get a
K&R book I suppose, for the full specs of the language?

K&R 2nd Ed. would be a good thing although it is not the full spec. of the
language. The full spec. is $18 USD on-line in PDF format if you really
want it.
 
T

Theo Stauffer

Mark said:
K&R 2nd Ed. would be a good thing although it is not the full spec. of the
language. The full spec. is $18 USD on-line in PDF format if you really
want it.
What would you suggest as a good book? I've got 'Practical C
Programming' from O'Reilly but it has no mention of things like function
pointers, sadly.
 
M

Micah Cowan

Theo Stauffer said:
Hi all, this is my first (utter C newbie) post, so I beg your patience.

I have a little code snippet:

typedef struct listtype {
struct list *list_ptr;
}list;

list l1,l2;


int main ()

{


l1.list_ptr = &l2;

}

When I try to compile this under Mac OSX, I get a compiler warning that
I'm assigning an incompatible pointer type. The strange and confusing
thing is that, in the 2 C tutorials I have they have conflicting
instructions on how to do this. The one says that it's fine, the other
doesn't.

There is no such type as struct list. You have struct listtype,
which is also known as list, but not struct list. However, you
have defined list_ptr as a pointer to this non-existant
(incomplete) type. This is why you get the compiler warning,
because they are in fact incompatible types. Change the
declaration of list_ptr to one of:

struct listtype *list_ptr;

or

list *list_ptr;

HTH,
Micah
 
M

Micah Cowan

Theo Stauffer said:
Thanks a million for that answer. I like C since it reminds me of
the Pascal that I learned about twenty years ago

*Wince*... please don't say that... ;-)

-Micah
 
T

Theo Stauffer

Micah said:
There is no such type as struct list. You have struct listtype,
which is also known as list, but not struct list. However, you
have defined list_ptr as a pointer to this non-existant
(incomplete) type. This is why you get the compiler warning,
because they are in fact incompatible types. Change the
declaration of list_ptr to one of:

struct listtype *list_ptr;

or

list *list_ptr;

HTH,
Micah
I actually had a typo in my snippet above. If I try list *list_ptr; it
won't compile either, although, as you say and the one tutorial I have
says, it should be ok. that is what's actually confusing me and made me
think it was a gcc version thing.
 
M

Mark A. Odell

What would you suggest as a good book? I've got 'Practical C
Programming' from O'Reilly but it has no mention of things like function
pointers, sadly.

I like K&R2, "Expert C Programming, Deep C Secrets", "Enough Rope to Shoot
Yourself in the Foot", "C traps and Pitfalls", and more I can't recall.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top