Using Null pointers

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.


And here's my code I wrote for exercise 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**********************************************************************************
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>

using namespace 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?
Last edited on
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 &
}
Last edited on
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.
Hi, Thanks for the explanations, just looking at from a different angle has helped a lot. I think I was more confused over the question.
Topic archived. No new replies allowed.