Archived

This topic is now archived and is closed to further replies.

MrBeaner

typdef struct || struct ?

Recommended Posts

MrBeaner    144
I am reading up on gametutorials.com some stuff, and was wondering the difference between these two statments: struct car { int RPM; int Fuel; //blah blah }; typdef struct _car { int RPM; int Fuel; //blah blah };CAR One is a structor called car, which, when i want to use it in code i would declare the type then the varibale name, as such: car Ferrari; the second is a structor as well, but has an alias as CAR, and if i want to create a car instance i would do the following: CAR Ferrari; Now, why would i want to use one over the other? This is where i am confused. They seem to do the same thing, but the only difference i can see would be that in the typedef i can declare a pointer to the type, where as i can''t in the first? Thanx _____________________________ Beer. Rocks.

Share this post


Link to post
Share on other sites
Fruny    1658
In C, you cannot use the tag (here car) of a struct alone to identify the type, you have to write struct car. Programmers being lazy, they declare a typedef to get rid of that constraint.

In C++ you can use the tag alone, without the struct (or class) keyword. Thus the typedef trick is less useful.

[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...

Share this post


Link to post
Share on other sites
MrBeaner    144
so it is really only applicable in C, but otherwise they ARE the same thing... very cool.. thanks a lot!

_____________________________
Beer.
Rocks.

Share this post


Link to post
Share on other sites
Creation    122
The latter (typedef) is C code, the former is C++. In C++ struct/class/union tags are automatically type names, so the typedef is unnecessary.

They''re similar, but unless you''re writing straight C code, use the former example.

Share this post


Link to post
Share on other sites
TerranFury    142
The former is the new C++ syntax. The latter is the old C syntax. They are equivalent. You mentioned something about not being able to create a pointer to the type using the first version; this is not the case. See this example:

  
struct vertex
{
float x, y;
};

vertex * vtxPtr = new vertex[128];
//...

delete [] vtxPtr;

Share this post


Link to post
Share on other sites
MrBeaner    144
So the general programming standards dictate which version i should use based on the language being implemented. Personally i like to look at as few words as possible, so i will use "stuct blah{}" in the future.

Thanks for the help, it is completly clear now. I was just confused as to why i was seeing it both ways...

_____________________________
Beer.
Rocks.

Share this post


Link to post
Share on other sites
Xanth    122
So then, this for instance...


  
struct point {
int x, y, z;
};

int addPoint(point* pt) {
return (pt->x + pt->y + pt->z);
}

int main() {
point p1 = {20, 20, 20};
int total = addPoint(&p1);
cout << total << "\n";

return 0;
}


Wouldn''t work in C?

Share this post


Link to post
Share on other sites
Xanth    122
hehe I ofcourse realize that I just wanted to post a working example.

Thanks for the answer though =)

struct Geek {
    void e_mail = belgedin@earthlink.net;
    void url;
    int age = 17;
};

[edited by - Xanth on May 3, 2002 12:36:42 AM]

Share this post


Link to post
Share on other sites