struct definition

This is a discussion on struct definition within the C Programming forums, part of the General Programming Boards category; I'm just wondering for structs if its possible to code it like this in this order Code: typedef struct { ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    struct definition

    I'm just wondering for structs if its possible to code it like this in this order

    Code:
    typedef struct
    
    {
    
       char   bug[10];
       struct Struct2*  structure;
    
    
    }Struct1;
    
    typedef struct
    {
      int size;
      struct Struct1* ptr;
    } Struct2;
    I want both the structs to have pointers of each other but Im worried the order will affect the compiling since in struct 1 the declartion of struct 2 wasnt define wont it just say it doesnt see it.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,787
    Quote Originally Posted by kiros88
    I want both the structs to have pointers of each other but Im worried the order will affect the compiling since in struct 1 the declartion of struct 2 wasnt define wont it just say it doesnt see it.
    You can find out if your worry was justified by writing a program to test it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    A problem that may arise is that there is actually no such thing as a "struct Struct2"; a Struct2 is user defined (typedef'd) datatype. But you can do this:
    Code:
    typedef struct _struct1
    {
       char   bug[10];
       struct _struct2 *ptr;    // cannot be Struct2 *ptr
    }Struct1;
    
    typedef struct _struct2
    {
      int size;
      Struct1 *ptr;      // could also be struct _struct1 *ptr
    } Struct2;
    You can know declare:

    Code:
    int main () {
        Struct1 alpha;
        Struct2 beta;
        alpha.ptr = β
        beta.ptr = α
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,787
    Quote Originally Posted by MK27
    A problem that may arise is that there is actually no such thing as a "struct Struct2"; a Struct2 is user defined (typedef'd) datatype. But you can do this:
    hmm... I was hoping that with an appropriate program a C compiler would warn about that, but apparently the MinGW port of gcc 4.4.0 does not. Consequently, I would point out an alternative:
    Code:
    typedef struct Struct2 Struct2;
    
    typedef struct Struct1
    {
        char bug[10];
        Struct2 *ptr;
    } Struct1;
    
    struct Struct2
    {
        int size;
        Struct1 *ptr;
    };
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    hmm... I was hoping that with an appropriate program a C compiler would warn about that, but apparently the MinGW port of gcc 4.4.0 does not.
    gcc -Wall -pedantic doesn't either. You can include this even:
    Code:
    typedef struct {
         struct ThisDoesNotExist *ptr;
    } mytype;
    As long as it is a struct it's all good...at this point.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,787
    Quote Originally Posted by MK27
    gcc -Wall -pedantic doesn't either.
    Of course that is what I tested with.

    Quote Originally Posted by MK27
    As long as it is a struct it's all good...at this point.
    I think you mean pointer, since a pointer can point to an incomplete type.
    Last edited by laserlight; 08-25-2009 at 11:58 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    I think you mean pointer, since a pointer can point to an incomplete type.
    Sure but this will not fly the same way:

    Code:
    typedef struct {
         ThisDoesNotExist *ptr;
    } mytype;
    I guess for a sort of unrelated reason, but the effect is still slightly misleading. No big deal.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,054
    So that suggests that a definition of a pointer to a struct type serves as a declaration for that struct. Interesting. Laserlight's code does a similar thing with a typedef using a yet undeclared struct.

    Is it valid C though, or just an extension?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    It's valid C.
    Quote Originally Posted by N1256 6.7.2.3#8
    If a type specifier of the form
    struct-or-union identifier
    occurs other than as part of one of the above forms, and no other declaration of the
    identifier as a tag is visible, then it declares an incomplete structure or union type, and
    declares the identifier as the tag of that type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  2. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21