string* pointerVariable = new string[10];
string name = "Squid";
pointerVaribale = &name;
But what does this mean?
In the first line, I allocated space for 10 strings. In the second line, I declared a variable. In the third line, I assigned the pointer to that variable.
However, didn't I allocate space for 10 strings? What does string[10] mean exactly?
string* pointerVariable = new string[10];
Means "I have this identifier that is a string pointer, and I will set it to point to an array of strings somewhere in memory. But at compile time, that memory address isn't allocated yet, so I will find some space in memory that isn't being used, and create that space."
string name = "Squid"
Means "somewhere in memory, allocate space for a string data type, and store a certain value in that memory location".
pointerVariable = &name
Means "instead of having my string pointer point to the 10 arrays of string, I will want to point it to another memory location". Though you do this, this means those 10 strings of arrays aren't deleted. You should probably delete the array of strings first.
One shouldn't use dynamic memory at all, unless one is creating a new kind of container. Use a smart pointer.
STL containers and classes like std::string already store their data dynamically, so no need to use new with them. Prefer to use a std::vector instead of plain arrays.
This can seem a lot harder than it needs to be. This is somewhat because everyone discusses pointers colloquially. Too much loose terminology.
In the first line, I allocated space for 10 strings.
Line 1 creates 10 new strings. Not merely space for those strings: memory space is allocated and 10 empty strings are constructed, or the operation failed (and an exception is thrown). Assuming success, pointerVariable is left pointing to the beginning of a block of memory containing 10 adjacent string objects.
Oh... so at the start the pointer is pointing at 10 memory locations, however, I changed the pointer so that it only points to 1 memory location?
Line 4 leaves pointerVariable pointing at the beginning of a block of memory containing one string object.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int how_many = 0;
cout << "I want to be able to store some strings" << '\n';
cout << "How many (make it a lazy 3) ? ";
cin >> how_many; // ENTER 3
// MAKE ENOUGH (new) SPACE AND A (*) POINTER TO THE FIRST ONE
string* pointerVariable_A = new string[how_many];
string* pointerVariable_B = new string[how_many];
// POPULATE THE ARRAY - ALTERNATIVE A
pointerVariable_A[0] = "one";
pointerVariable_A[1] = "two";
pointerVariable_A[2] = "three";
for(int i = 0; i < how_many; i++)
cout << pointerVariable_A[i] << '\n';
cout << '\n';
// POPULATE THE ARRAY - ALTERNATIVE B
*pointerVariable_B = "four";
*(pointerVariable_B + 1) = "five";
*(pointerVariable_B + 2) = "six";
for(int i = 0; i < how_many; i++)
{
cout << *pointerVariable_B << '\n';
pointerVariable_B++;
}
cout << '\n';
string name = "Squid";
pointerVariable_A = &name;
cout << *pointerVariable_A << '\n'; // DEREFERENCING (*)
return 0;
}