Because they do two different things.
An array creates memory:
char p[ 4 ];
That creates a block of memory four bytes long. You can get the address of the beginning of the block of memory by using
p directly, because the compiler will automatically convert it to a pointer for you. Hence,
strlen( p );
works, passing the address of the first char in p[].
A pointer only accesses memory:
char* q;
That creates a pointer to some block of memory, somewhere. Maybe.
Unlike before, where memory was allocated for your array, we now just have a place to keep an address. As of yet, it doesn't point anywhere meaningful. That's why you should initialize to NULL.
char* q = NULL;
Now we know can check for
sure that it doesn't point anywhere before trying to use it:
1 2 3
|
int len;
if (q) len = strlen( q );
else len = 0;
|
Before you use it, you must make it reference memory that has been allocated for you. You can use the address of something that exists:
q = p;
or you can create a new block of memory:
q = new char[ 4 ];
(which you must later free with
delete [] q;
).
Hope this helps.