tags:

views:

90

answers:

3

if I make a typedef someobject* pntr;

I can use this typedef to make a pointer to point to an object of type someobject. but can I also use a pointer made from this typedef to point to an array of someobject?

+2  A: 

Yes:

pntr p = new someobject[10];

However, it is considered poor style in both C++ and C to use typedefs to disguise the fact that something is a pointer, so don't do this.

anon
Or name it such that you know that it is a pointer, like `someobject_ptr` or `SomeObjectPtr` or whatever your style is. This is commonly used in APIs.
St3fan
A: 

yes. The array syntax is really just specifying a memory address to look for your types (objects). Using the typedef will reveal how far to move ahead (in memory) when calculating the pointer math for your type.

e.g. if you have an array of someObject, the compiler needs the typedef info to move the pointer ahead to the correct spot in memory when you go from say someObjectArray[0] to someObjectArray[1]

darren
+1  A: 

Yes, but don't typedef pointer types. const pntr and const someobject* would not be the same thing (the former means that the pointer itself is constant; the latter means that the pointee is constant, which is what is usually desired).

jamesdlin