Results 1 to 3 of 3

Thread: Struct/typedefs

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Struct/typedefs

    I have seen code like this in some places:
    Code:
    typedef struct _mystruct {
        int x;
        int y;
    } mystruct;
    But, in my C++ class at school, the teacher told us to do it like this (no typedef):
    Code:
    struct mystruct {
        int x;
        int y;
    };
    Why would you do the first one if the second one works?
    Alcohol & calculus don't mix.
    Never drink & derive.

  2. #2
    Zaei
    Guest
    In C, if you used:
    Code:
     struct <name> { // stuff };
    You would have to declare variables of <name> like so:
    Code:
    struct <name> myVar;
    When using the typedef, you eliminate the extra struct.

    Z.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    In C++, this weird syntax is no longer necessary. So it is absolutely understandable if your teacher tells you not to do it (it still works). But he should have told you why they did it otherwise in C.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width