this is just a small program i wrote to practice using pointers, and passing them into functions, and also showing that you can modify the values in an address using a pointer by purposely getting the last name wrong. I just want to know if im doing this properly and im actually showing that the value is in fact being modified.
so your saying that rather than call printGreeting(p_first, p_last); in void getInfo(string newFirst, string newLast)... i should be calling it in main()?????
also i understand that passing parameters using reference is the better idea, but the whole purpose of this is that i am learning pointers and how to properly use them so this is strictly educational as i am a beginner.
I don't think the strings you are passing to getinfo() are doing anything. They are empty strings and are being passed by value ( a copy). newfirst is a local variable and is getting assigned the empty string you passed by value. It does nothing with this empty string and then gets assigned the value from cin.
You should do the prototypes either getinfo(string& first, string& last) or getinfo(string *first, string *last)
Then call the function in nmain with getinfo(first,last); (in the reference version- first case)
or
getinfo(&first,&last); (if you use the pointer version- second case)
That way the strings you are passing from main will be changing and you can test this with cout statement in main.
@MasartiDeluxe: Sorry: I misread that you were calling that function to see the results of your getInfo() function. Forget what I said about testing from main -- You already had a function to test the result of getInfo().
Do you get any warnings in print Greeting() in your new version?
Yes, now you are changing the strings you declared in main with both functions. You pass both strings by reference to getInfo() and you assign them the input from cin. To return to main and you pass pointers to these strings to printGreeting(). In prinGreeting you change the string you declared in main when you assign "Caughing" to the string that p_newLast is pointing to.
thanks! i did add your code and it does modify the value in string last; which is exactly what i wanted to see, i didnt think to do that..... but it was good for me to see that change so i can better understand pointers thanks again!