Thread: Question about structs

  1. #1
    Evil Member
    Join Date
    Jan 2002
    Posts
    638

    Question about structs

    I'm going back into pure c for a while, 'cuz of this stupid class, and I was wondering...

    Why do some people use:

    Code:
    struct foo {
    /* Members */
    };
    Whereas other use:

    Code:
    typedef struct {
    /* Members */
    } foo;
    Is there any advantage to one over the other, or is it just a case of preference?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    To create an instance of the first you must use:

    struct foo inst;

    Whereas the second allows you to omit the struct since foo is a type alias for it:

    foo inst;

    In this case it really is a matter of personal preference.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    And then there's :

    typedef struct foo {

    };



    int main() {

    foo bar;

    return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    typedef struct foo
    {
    
    };
    
    int main()
    {
      foo bar;
      return 0;
    }
    This is not a standard construct.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well it sure compiles fine

    Anyway, I like it because of it's similarity to the C++ syntax for declaring classes. Very convenient.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    aurė entuluva! mithrandir's Avatar
    Join Date
    Aug 2001
    Posts
    1,209
    The reason why you don't have to use typedef when using struct is because that typedef used on its own does not imply a struct. Remember that you can use typedef for other purposes.

    This is best explained here.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Sebastiani
    Well it sure compiles fine
    Code:
    void main( )
    {
       malloc( 1000000 );
    }
    This compiles fine too, it doesn't mean I'd use it.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Structs question
    By BigFish21 in forum C Programming
    Replies: 25
    Last Post: 04-23-2008, 09:57 PM
  3. Replies: 5
    Last Post: 02-20-2004, 09:36 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM