When we create a array of integers we do it like this:
int main() {
int x;
cout << "Enter size of array"
cin >> x;
int* myArray;
myArray = new int[x]
}
we assign the asterisks next to array, we are assigning it as a array of pointers right?
If I make a array of nodes where:
struct Node {
string Name;
int Age;
}
I ran some code to experiment with it and understand it more and I tried to do just like the array sample but create a array with nodes.
int main() {
Node* sumShit[5];
Node* America = new Node();
America->age = 16;
America->Name = "America";
sumShit[0] = America;
Node* Japan = new Node();
Japan->age = 15;
Japan->Name = "Japan";
sumShit[1] = Japan;
cout << "[" << sumShit[1]->Name << ", " << sumShit[1]->age << "]";
}
Everything printed out fine with pointers but then I did it also without pointers, where I just stored node properties in the Node:
Node myNodeShit[5];
Node Poop;
Poop.age = 16;
Poop.Name = "Poop";
myNodeShit[0] = Poop;
sortArrayName(myNodeShit, 5);
printArray(myNodeShit, 5);
And this also worked, however whats the advantages to using pointers and just storing it within the node. When it comes to algorithms, sorting and using memory, is there a preferred way. Im trying to figure why it would be better to have it as a array of pointers to nodes vs a array of nodes.
newinside the function, and then returned that pointer, then it will be okay since the memory will still be allocated when the function returns.