Advertisement

typdef struct || struct ?

Started by May 03, 2002 02:51 PM
8 comments, last by MrBeaner 22 years, 7 months ago
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.
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
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...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
so it is really only applicable in C, but otherwise they ARE the same thing... very cool.. thanks a lot!

_____________________________
Beer.
Rocks.
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
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.
--------------------------{ Arena: Resurrection, my text-based RPG/Mortal Kombat-like game }{ A Look Into The World Of Arena II, a collection of short stories about characters in Arena II }
Just a side comment: in C++ you can think of a struct as being an "all public" class.
Forever trusting who we areAnd nothing else matters - Metallica
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;  
Advertisement
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.
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
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?

"I thought Genius lived in bottles..." - Patrick Star

  cout << total << "\n";  


this is C++ already.

Back to your Q: no, it wouln''t (even w/o that cout...)
Forever trusting who we areAnd nothing else matters - Metallica
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]
"I thought Genius lived in bottles..." - Patrick Star

This topic is closed to new replies.

Advertisement