Is there any difference?

This is a discussion on Is there any difference? within the C++ Programming forums, part of the General Programming Boards category; Is there any difference in a typedef struct and a normal struct? This works just as expected either way: Code: ...

  1. #1
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949

    Is there any difference?

    Is there any difference in a typedef struct and a normal struct? This works just as expected either way:
    Code:
    #include <iostream>
    using namespace std;
    
    typedef struct {
    	int x, y;
    } Point;
    
    struct Point2 {
    	int x, y;
    };
    
    int main() {
    	Point p;
    	Point2 p2;
    	p.x = 3;
    	p.y = 2;
    	p2.x = 8;
    	p2.y = 45;
    	cout << p.x << " " << p.y << " " << p2.x << " " << p2.y << endl;
    	return 0;
    }
    Is there any benefit from any way? Thanks !
    Do not make direct eye contact with me.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I believe that the difference is just that in C, you must use typedef, where as in C++, it is optional.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    It's required?? I'm guessing that's changed in C99 - or am I wrong there, too?
    Do not make direct eye contact with me.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I'm not sure about in C99 but it did not used to be required to use typedef. You would use the typedef version if you wanted to declare the struct like:

    Point p;

    If you did not use typedef you had to use:

    struct Point p;

    May have changed in C99, not sure.

    edit: In case of any confusion I'm talking about C here not C++ obviously.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Right, that seems appropriate.
    Do not make direct eye contact with me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Review required for program of date difference
    By chottachatri in forum C Programming
    Replies: 6
    Last Post: 10-31-2008, 11:46 AM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. how to get difference of digits
    By Leeman_s in forum C++ Programming
    Replies: 5
    Last Post: 12-20-2001, 07:32 PM
  5. What is the Difference between ANSI C and non-ANSI C ?
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-24-2001, 05:55 AM

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