Thread: propper way of declaring a struct for a linked list or binary tree

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    propper way of declaring a struct for a linked list or binary tree

    i have seen several ways of doing this
    Code:
    typedef struct some_name
    {
       // variables here
    } some_name;
    
    typedef struct some_name
    {
        //variables here
    } some_other_name; // this is used later on to address this struct
    
    struct some_name // to use this they write struct some_name myvar;
    {
        //variables here
    };
    
    struct some_name 
    {
        //variables here
    };
    
    typedef struct some_name myvar
    //then type my_var->some memeber
    which is the correct way or are they all equally valid and down to preference
    many thanks
    coop

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Unless I'm referring to the struct inside itself, I always use this:
    Code:
    typedef struct {
       // variables here
    } some_name;
    If I have to though, I give it a "reserved" name:
    Code:
    typedef struct _some_name {
       // variables here
       struct _some_name *ptr;
    } some_name;
    Devoted my life to programming...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by GReaper
    If I have to though, I give it a "reserved" name:
    You should be aware though:
    Quote Originally Posted by C11 7.1.3 Paragraph 1 (part)
    All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.
    For struct _some_name, the name is presumably declared at file scope, and _some_name is in the tag name space of struct. Therefore, the name is not merely "reserved", but really is reserved to the implementation.
    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

  4. #4
    Registered User
    Join Date
    May 2019
    Posts
    214
    Most regard this as the "modern" C style

    Code:
    struct some_name // to use this they write struct some_name myvar;
    
    {
    
        //variables here
    
    };
    Yet, it requires the use of struct to use it to declare a variable.

    The typedef defines a type so the struct keyword isn't required to declare a variable.

    Code:
    typedef struct        
    {
      int x;
      int array[100];
    } Foo;
    
    Foo a;
    
    typdef Foo AnotherFoo;
    
    AnotherFoo a;
    
    typdef some_name myname;
    
    myname b;
    struct some_name c;
    Last edited by Niccolo; 06-12-2019 at 05:51 AM.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by cooper1200 View Post
    Code:
    struct some_name 
    {
        //variables here
    };
    
    typedef struct some_name myvar
    //then type my_var->some memeber
    This is wrong... You are defining a type, not a var.

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    [QUOTE=flp1969;1287511]This is wrong... You are defining a type, not a var.[/QUOTE

    sorry line 7 of your post should read then they can type: myvar->some_member

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    To understand flp1969's objection, try compiling:
    Code:
    #include <stdio.h>
    
    struct some_name 
    {
        int some_member;
    };
    
    typedef struct some_name myvar;
    
    int main(void)
    {
        myvar->some_member = 123;
        printf("%d\n", myvar->some_member);
        return 0;
    }
    Then compile and run:
    Code:
    #include <stdio.h>
    
    struct some_name 
    {
        int some_member;
    };
    
    typedef struct some_name myvar;
    
    int main(void)
    {
        myvar my_real_var;
        myvar *my_real_pointer = &my_real_var;
        my_real_pointer->some_member = 123;
        printf("%d\n", my_real_pointer->some_member);
        return 0;
    }
    Basically, you cannot validly write myvar->some_member because myvar is a type, not a variable of pointer type.
    Last edited by laserlight; 06-12-2019 at 06:47 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

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    oh i get it now sorry.... i meant the latter sorry i didn't word it correctly at all

    it was written on the fly quickly because every time i watch a video one of versions 2, 3 or 4 (the correct version of 4) but never the way i do it (version 1) i guess its application and user dependant

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by laserlight View Post
    You should be aware though:
    Quote Originally Posted by C11 7.1.3 Paragraph 1 (part)
    All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.
    For struct _some_name, the name is presumably declared at file scope, and _some_name is in the tag name space of struct. Therefore, the name is not merely "reserved", but really is reserved to the implementation.
    I knew that, but I've shamelessly ignored it so far. I guess it hasn't presented as a problem since I haven't been a part of any major project. Could it actually create problems? What might you suggest as an alternative?
    Devoted my life to programming...

  10. #10
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by cooper1200 View Post
    Quote Originally Posted by flp1969 View Post
    This is wrong... You are defining a type, not a var.
    sorry line 7 of your post should read then they can type: myvar->some_member
    Note, you don't get it... the right way, in this case, is:
    Code:
    struct some_name { ... };
    typedef struct some_name mytype;
    
    mytype myvar; // myvar.some_member

  11. #11
    Registered User
    Join Date
    May 2019
    Posts
    214
    @GReaper

    What might you suggest as an alternative?
    A trailing underscore.

  12. #12
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by GReaper View Post
    I knew that, but I've shamelessly ignored it so far. I guess it hasn't presented as a problem since I haven't been a part of any major project. Could it actually create problems? What might you suggest as an alternative?
    I generally give the struct and the typedef type the same name:
    Code:
    typedef struct some_name {
        // variables here
        struct _some_name *ptr;
    } some_name;
    There's no name conflict because "struct" is a separate namespace from other types. It works in C++ too (though it's unnecessary because C++ automatically lets you use a struct tag without "struct" in front of it).

  13. #13
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    christop that's exactly what i do but in all the videos i have watched i have never seen it done

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Some old C compiler had a name clash when you did it that way.

    Edit: This might have been in the K&R C days

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  15. #15
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by stahta01 View Post
    Some old C compiler had a name clash when you did it that way.

    Edit: This might have been in the K&R C days

    Tim S.
    Yeah, it might be a problem with a pre-ANSI compiler, but fortunately it should work fine with almost any standard C compiler made in the last 30 years.

    (It's definitely something to be aware of if you do software archaeology, like looking through the ancient Unix source code or something.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list binary tree..
    By scarlet00014 in forum C Programming
    Replies: 9
    Last Post: 11-23-2008, 11:19 PM
  2. Tree of Struct with Linked List
    By jjeanie in forum C Programming
    Replies: 4
    Last Post: 05-03-2007, 06:36 PM
  3. Binary Tree -vs- Linked List
    By dbnelson2403 in forum C Programming
    Replies: 7
    Last Post: 02-08-2006, 08:59 AM
  4. Linked List or Binary Search Tree
    By rhysmeister in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 03:04 PM
  5. Linked List To Binary Tree
    By DaJoe in forum C Programming
    Replies: 3
    Last Post: 05-12-2002, 10:59 PM

Tags for this Thread