Forget the arrays and pointers in that one. You question is more general and is about names.
Those are not
similar. A
p
is
identical to
p
, if they are in the same scope.
One cannot declare the same name more than once in the same scope.
However, one can declare the same name in an inner scope, in which case it
masks identical name of an outer scope. Masking is confusing. Avoid doing it.
Local names in the scope of one function are entirely different from local names in the scope of another function. Therefore, the p in function1 is not at all similar to the p in function2.
Once again, there is nothing that prevents you from using pointer p to point first to one array and later to point to a different array within same function.
This is entirely legal:
1 2 3 4 5 6 7 8 9
|
int x;
int * p = nullptr;
p = new int[7];
delete [] p;
p = &x;
p = new int;
delete p;
p = new int[9];
delete [] p;
|
But why use raw pointer,
new
and
delete
?
You could use smart pointer, like
std::unique_ptr
(there is
std::make_unique
for it), or container like
std::vector
.