char *p = new char[10];
dynamically allocates a memory block for an array of size of 10 chars and returns the address of its first element which is then stored into p
(making p
to point to the beginning of this memory block).
In this case, the new
keyword is followed by an array type specifier, you specify the type:
char **p = new char*[10];
- type in this case is char*
, not (char*)
. Check operator new[]
You are probably confused because of C-style malloc
syntax where the type of its return value is always void*
, which can be cast to different type so that you can dereference it. That's the situation where you use (char*)
syntax (C-style type cast): char *p = (char*) malloc(10);
Although note that in C this cast is redundant: Do I cast the result of malloc?
Note that memory that was allocated by using new[]
should be freed by calling delete[]
and memory allocated by malloc
should be freed by free
.