Pointers in structures II (newbie question)

Discussion in 'C Programming' started by Theo Stauffer, Sep 30, 2003.

  1. 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
     
    Theo Stauffer, Sep 30, 2003
    #1
    1. Advertising

  2. Theo Stauffer <> wrote in
    news:3f79cd47$:

    > 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 ->
    --
     
    Mark A. Odell, Sep 30, 2003
    #2
    1. Advertising

  3. Mark A. Odell wrote:

    > Theo Stauffer <> wrote in
    > news:3f79cd47$:
    >
    >
    >>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?
     
    Theo Stauffer, Sep 30, 2003
    #3
  4. Theo Stauffer <> wrote in
    news:3f79d815$:

    > 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 ->
    --
     
    Mark A. Odell, Sep 30, 2003
    #4
  5. Mark A. Odell wrote:

    > Theo Stauffer <> wrote in
    > news:3f79d815$:
    >
    >
    >>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
    ------
     
    Theo Stauffer, Sep 30, 2003
    #5
  6. Theo Stauffer

    Micah Cowan Guest

    Theo Stauffer <> 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
     
    Micah Cowan, Oct 1, 2003
    #6
  7. Theo Stauffer

    Micah Cowan Guest

    Theo Stauffer <> 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
     
    Micah Cowan, Oct 1, 2003
    #7
  8. Micah Cowan wrote:

    > Theo Stauffer <> 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
    ------
     
    Theo Stauffer, Oct 1, 2003
    #8
  9. Theo Stauffer

    Ben Pfaff Guest

    Micah Cowan <> writes:

    > Theo Stauffer <> 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
     
    Ben Pfaff, Oct 1, 2003
    #9
  10. Theo Stauffer <> wrote in
    news:3f7a06c1$:

    > 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 ->
    --
     
    Mark A. Odell, Oct 1, 2003
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    420
  2. DSKR

    Pointers to 'Data Structures' in C

    DSKR, Jun 24, 2003, in forum: C Programming
    Replies:
    31
    Views:
    10,080
    Giuseppe
    Jul 3, 2003
  3. tweak
    Replies:
    14
    Views:
    2,788
    Eric Sosman
    Jun 11, 2004
  4. Alfonso Morra
    Replies:
    11
    Views:
    721
    Emmanuel Delahaye
    Sep 24, 2005
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    682
Loading...

Share This Page