Reputation: 53
Can I define pointer to a string array like this?
std::string* str_arr_p[];
Or I need to set the size of the array?
P.S.: Is there any difference between std::string* str_arr_p[n];
and std::string (*str_arr_p)[n];
Upvotes: 0
Views: 86
Reputation: 13752
Can I initialize pointer to a string array like this?
std::string* str_arr_p[];
Or I need to set the size of the array?
No you can't. Try it and see that you get compilation error.
Is there any difference between
std::string* str_arr_p[n];
andstd::string (*str_arr_p)[n];
Yes there is. The first is an array of n
pointers to std::string
, the second is a single pointer to an array of std::string
of size n
.
Note that both are uninitialized (if not declared as global or static), i.e. both point or hold arbitrary address(es). The first holds n
arbitrary addresses that can be initialized separately with a valid address or nullptr
for each of the n
pointers in the array. The second is a single address that need to be initialized with an address of an std::string
array which has the exact size n
.
Upvotes: 1
Reputation: 944
std::string* str_arr_p[n];
is an array of pointers to std::string
.
std::string (*str_arr_p)[n];
here str_arr_p is a pointer to an array of n std::string
std::string* str_arr_p[];
this is a definition, you don't initialize anything.
std::string* str_arr_p[] = new std::string* [x];
heap allocated or
std::string* str_arr_p[50];
Stack allocated, this will be destroyed when function ends
Upvotes: 1