I'm working through some examples and I've reached pointers, I'm kind of understanding. But I am so stuck when it comes to Uninialized Pointers. This is what I have to do:
Modify the program you wrote for exercise 1 so that instead of always prompting the user for a last name, it does so only if the caller passes in a NULL pointer for the last name.
Allain, Alex (2013-09-16). Jumping into C++ (p. 180). Cprogramming.com. Kindle Edition.
/**********************************************************************************
Write a function that prompts the user to enter his or her first name and last name,
as two separate values. This function should return both values to the caller via
additional pointer (or reference) parameters that are passed to the function.
Try doing this first with pointers and then with references.
(Hint: the function signature will look similar to the swap function from earlier!)
**********************************************************************************/
#include <iostream>
#include <string>
usingnamespace std;
void userName(string *p_firstname, string *p_secondname){
cout << "Enter first name: ";
getline(cin, *p_firstname, '\n');
cout << "\nEnter second name: ";
getline(cin, *p_secondname, '\n');
}
int main(){
string firstname;
string secondname;
//string p_secondname = NULL;
userName(&firstname, &secondname);
cout << "\n\n" + firstname + " " + secondname;
}
What I want to know is do I write the pointer for secondname is = NULL in the main function, username function or globally?
You can think of NULL as a special pointer, which usually points to memory address 0.
In the current standard of C++ (which is C++11 of year 2011), instead of NULL you should use nullptr.
1 2 3 4 5 6
// ...
int main()
{
userName(NULL, nullptr); // passing the null pointers directly
}
To do what I think you want to do, p_secondname should be a pointer to std::string and not simply an std::string.
1 2 3 4 5 6 7 8 9
// ...
int main()
{
string *p_firstname = NULL;
string *p_secondname = nullptr;
userName(p_firstname, p_secondname); // notice that there are no ampersands &
}
prompt the user for a last name only if the caller passes in a NULL pointer
That is a big one.
Variables are names for values that are stored in memory. Pointer is a variable that stores a memory address. In such memory location there might be a value. The value can be reached by dereferencing the pointer.
Null pointer contains no valid address. There is no memory address that could be accessed by dereference.
Here is a much simpler task:
"prompt the user for a last name only if the caller passes in a valid pointer"
Valid pointer means valid memory location, where a new "lastname" can be written to.
No such luxury with your case. You have to allocate memory for the answer and pass the address to the pointer variable of the caller. Therefore, the function cannot take a plain pointer parameter; it has to take a pointer or reference to a pointer. Furthermore, you have to ensure appropriate deallocation of the memory.