Pointer to array ?

The CPP Programming Lanuage chapter 5, exercies 3, use typedef to define pointer to arrays.

Does pointer to array exist ?
I tried
typedef (char *pchar_array)[];

but the compiler give the error: syntax error : ')'.

When I learned C, I learn that if I defined int a[10], a is only a symbol of address, not an object. But in VC, &a is valid and has the same value as a itself. Is C++ different from C at this point ?
int (*a)[10];

is a pointer to an array of 10 ints.

typedef int (*PtrArray)[ 10 ];

defines a pointer-to-an-array-of-10-ints type.
Thanks jsmith.
Another question is, is array an object in C++ ? Like int a[10], a is not only an address, but also has a memory unit ?
1
2
3
4
5
6
7
8
#include <vector>

using namespace std;

int a[10];  // C array: chunk of contiguous memory
int (*b)[10];  // pointer to a chunk of contiguous memory
vector<int> c( 10 );  // C++ array: an object - memory allocated through a constructor
vector<int>* d;  // pointer to an object 


Think of a being an address as a convenience that the C language offers you, so you don't have to type &a[0]. Whenever you get confused, ask yourself: how much memory has just been allocated with my declaration?

In general, for simplicity, you can think of C++ is a superset of C, with some caveats:

http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B
Last edited on
Topic archived. No new replies allowed.