Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Pointers in structures II (newbie question)

Reply
Thread Tools

Pointers in structures II (newbie question)

 
 
Theo Stauffer
Guest
Posts: n/a
 
      09-30-2003
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


 
Reply With Quote
 
 
 
 
Mark A. Odell
Guest
Posts: n/a
 
      09-30-2003
Theo Stauffer <(E-Mail Removed)> wrote in
news:3f79cd47$(E-Mail Removed):

> 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];

--
- Mark ->
--
 
Reply With Quote
 
 
 
 
Theo Stauffer
Guest
Posts: n/a
 
      09-30-2003
Mark A. Odell wrote:

> Theo Stauffer <(E-Mail Removed)> wrote in
> news:3f79cd47$(E-Mail Removed):
>
>
>>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?

 
Reply With Quote
 
Mark A. Odell
Guest
Posts: n/a
 
      09-30-2003
Theo Stauffer <(E-Mail Removed)> wrote in
news:3f79d815$(E-Mail Removed):

> 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.

--
- Mark ->
--
 
Reply With Quote
 
Theo Stauffer
Guest
Posts: n/a
 
      09-30-2003
Mark A. Odell wrote:

> Theo Stauffer <(E-Mail Removed)> wrote in
> news:3f79d815$(E-Mail Removed):
>
>
>>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.
>

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.

--
------
Theo
------

 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      10-01-2003
Theo Stauffer <(E-Mail Removed)> writes:

> 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
 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      10-01-2003
Theo Stauffer <(E-Mail Removed)> writes:

> 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
 
Reply With Quote
 
Theo Stauffer
Guest
Posts: n/a
 
      10-01-2003
Micah Cowan wrote:

> Theo Stauffer <(E-Mail Removed)> writes:
>
>
>>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

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.

--
------
Theo
------

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      10-01-2003
Micah Cowan <(E-Mail Removed)> writes:

> Theo Stauffer <(E-Mail Removed)> writes:
>
> > 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...


Here, have another:
#define begin {
#define end }
--
"This is a wonderful answer.
It's off-topic, it's incorrect, and it doesn't answer the question."
--Richard Heathfield
 
Reply With Quote
 
Mark A. Odell
Guest
Posts: n/a
 
      10-01-2003
Theo Stauffer <(E-Mail Removed)> wrote in
news:3f7a06c1$(E-Mail Removed):

> 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.

--
- Mark ->
--
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
A question regarding the duality of pointers and arrays and another on structures. Squignibbler@hotmail.com C++ 7 03-26-2005 09:09 PM
Type Casting IPv4 and IPv6 structures to Generic Structures tweak C Programming 14 06-11-2004 02:43 PM
Pointers to 'Data Structures' in C DSKR C Programming 31 07-03-2003 04:49 PM



Advertisments