Question about structs

This is a discussion on Question about structs within the C Programming forums, part of the General Programming Boards category; I'm going back into pure c for a while, 'cuz of this stupid class, and I was wondering... Why do ...

  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,796
    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,707
    And then there's :

    typedef struct foo {

    };



    int main() {

    foo bar;

    return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool 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,796
    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,707
    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 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, 08: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, 08:22 PM

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