Hey,
let's check it out step by step ;)
A pointer stores adresses, what you're doing in the following is this:
You give the adress from Name[20] to your pointer.
Your "char-Array-name" goes from Name[0] to Name[19] which means your name "Brian"
begins at Name[0] with 'B' and goes on from there
We have no clue what happens to be at "Name[20]" because we didn't put anything there (it is even out of bounds of our array)
What we want to achieve is to give our pointer the adress of the first character in name = 'B' and that happens to be Name[0]
1 2 3 4 5
|
NamePtr = &Name[CHARARRAY];
//My bla-bla above means we change what you wrote above this comment to:
NamePtr = &Name[0]; //Give our pointer to adress of the start => 'B'
//Actually you can also write
NamePtr = Name; //but that's probably confusing (some people are even angry that it works)
|
The next line is overwriting what we did before with a new empty char Array
It doesn't really make any sense to put this code-line here => we will just comment it out
|
//NamePtr = new char[CHARARRAY];
|
This is actually totally fine
..if it weren't for this line
Again you overwrite the adress you gave the Age-Pointer above with some
uninitialized int which happens to be "-842150451" in your output
=> just remove this line ;)
|
//AgePtr = new int; //bye bye line of code
|
1 2 3 4 5
|
//I removed the * before NamePtr => with the * it just prints "B"
//Without it knows it has to print a string => Brian
//Age stuff is cool, good job
cout << " This Persons name is " << NamePtr
<< " and their age is " << *AgePtr << endl;
|
Have a nice day
Edit: and remove those "delete" lines they're just not right at this point (should probably result in an error)