# what is the typdef statement mean?

## Recommended Posts

narnia649    138
For example: typedef int Map[MapSizeX][MapSizeY]; what does that mean? Where can I learn about typedef? Is it like a class?

#### Share this post

##### Share on other sites
That example typedef is broken. :-)

It lets you create another name that refers to the same type as an existing one.

e.g. typedef unsigned uint;

Means that I can now do

uint i;

as opposed to

unsigned i; (equivalent to unsigned int i;)

Note: it introduces a synonym - it doesn't replace the existing name (so both work). Standard scoping rules apply.

It's primarily useful if you're using rather lengthy type names, for the sake of making reading easier (particularly if you're working with iterators of templated types, such as std::vector<std::vector<std::pair<unsigned, std::string> > >::const_iterator - although that is a somewhat contrived example).

For more information, simply plugging 'typedef' into Google will yield plenty.

[Edited by - TheUnbeliever on May 17, 2008 11:07:28 AM]

#### Share this post

##### Share on other sites
DevFred    840
Quote:
 Original post by TheUnbelieverThat example typedef is broken. :-)

Hm, it seems fine to me. Map is introduced as a new name for int[MapSizeX][MapSizeY]. The syntax may seem strange at first glance, but I don't see any errors.

#### Share this post

##### Share on other sites
Dbproguy    108
In my mind I just see typedef as being like a shortcut.

Example:
typedef unsigned short int ushort;

That code, if placed before the start of your main function will create a shortcut, so when you type
ushort cityPopulation

then it will make cityPopulation an unsigned short.

#### Share this post

##### Share on other sites
Quote:
Original post by DevFred
Quote:
 Original post by TheUnbelieverThat example typedef is broken. :-)

Hm, it seems fine to me. Map is introduced as a new name for int[MapSizeX][MapSizeY]. The syntax may seem strange at first glance, but I don't see any errors.

Does it not need to be

typedef int[MapSizeX][MapSizeY] Map;

rather than

typedef int Map[MapSizeX][MapSizeY];

though?

EDIT: Apparently not (the first spews 8 errors, the second none). You learn something every day, I guess!

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now