typedef question

this seems like a silly question but i'm working through a game scripting book and see alot of

typedef struct structname
{
int a,
int b,
...
} typedefname

now what is the benefit of using a typedef here? either way , you reference the struct with the same amount of variables

structname.a = ...
typedefname.a = ...

is that right? am i missing something?
You're probably reading a book on the C Programming Language rather than C++.

In C, struct's go in their own namespace, and so you need to use the full name. For example, struct stat.

Another example:
1
2
3
4
5
6
7
8
9
struct structname
{
    int a;
    int b;
};

struct structname s;
s.a = 1;
s.b = 2;


As you can see, it's noise. So the name is often typedef'd save you writing struct everywhere.

C++ does not treat struct in the same way. You never need to use struct when using a C++ structure, so the practice is redundant.
thanks!
typedefs are used widely in C++ to abbreviate and give more readable names to STL constructs e.g.

1
2
typedef std::vector<string> strings; 
typedef std::map<string,string> hashtable;


in C++ struct is often used to get the default public attribute on all members of the 'class' e.g. when defining an interface

1
2
3
4
5
struct visitor
{
    visit( somedata& a);
    visit( someotherdata& b);
};

Topic archived. No new replies allowed.