I have a pointer array of strings:
string *names = new string[numOfNames];
That array is then populated with values from a file. I am trying to sort the names alphabetically so I am trying to assign another pointer array to the address of each individual element names is pointing to. So I want to do something like this:
string *namesPtr = new string[numOfNames];
namesPtr[0] = &names[0];
However I am getting a red squiggly on the equal sign saying
no operator "=" matches these operands, operand types are std::string = std::string*
I have a pointer array of names because numOfNames will be specified in the file I am going to open and from my understanding, a regular array won't work because I have to specify the number of elements in the array.
Of course there is a type mismatch.
Type of namesPtr[0]
is string
.
Type of &names[0]
is string*
.
What you need is:
string** namesPtr = new string*[numOfNames];
namesPtr[0] = &names[0];
To make the pointer assignments complete, you need:
for (int i = 0; i < numNames; ++i )
{
namesPtr[i] = &names[i];
}
namesPtr[0] = &names[0];
This is incorrect. & will produce the address of the dereferenced string in names, thus you are assigning an address to a string which is illegal.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.