Passing NULL pointers as function argument

I'm working through some practice problems and i might not have understood are have done the exercises corrected, after doing some searches online i didn't find a solution to my quest. the problem has me write code to ask the user for his/her first name and a last name, but only ask for the last name if the pointer is NULL. this is what i got and can't wrap my head around passing NULL, how can i pass NULL and still have it change the string variable for last name?


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
31
  #include <iostream>
#include <string>

using namespace std;

void User_Name (string *p_first_name, string *p_last_name )
{
    cout << "Enter first name: ";
    cin >> *p_first_name;
    cout << endl;

    if (p_last_name == NULL){
        cout << "Enter last name: ";
        cin >> *p_last_name;
    }


}

int main ()
{
    string first_name;
    string last_name;

    string *p_first_name = NULL;
    string *p_last_name = NULL;

    User_Name(&first_name, &last_name);

    cout << endl << endl << "User name: " << first_name << " " << last_name;
}
If you access the memory at a null pointer, the behavior is undefined. This is because it doesn't point to anything.

You can create something for the pointer to point to, but even so that requires that you change the caller's pointer itself, which you cannot do given the function signature above.

Did you misread the prompt? Pointers are often used to specify arguments that may be null, without implying a transfer of ownership (if they may not be null, a reference is a superior choice). In the case where an argument is null, you must not access the pointed-to-memory. Conventionally, this manifests as a phrase like "if x is null, take the default action/do nothing" which often appears in documentation.

P.S.:
In modern C++, the macro NULL should be replaced with the language keyword nullptr.

Maybe you should link the entire problem.
Last edited on
mbozzi thanks, and here is the practice problems:

practice problem 1:
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 (the caller is main , right ?) via additional pointer (or reference) parameters that are passed to the function. Try doing this first with pointers and then with references.

practice problem 2:
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// problem one
void User_Name( std::string& first_name, std::string& last_name )
{
    std::cout << "Enter first name: ";
    std::cin >> first_name;

    std::cout << "Enter last name: ";
    std::cin >> last_name;
}

int main()
{
    std::string first_name;
    std::string last_name;

    User_Name( first_name, last_name);

    std::cout << "\n\nUser name: " << first_name << ' ' << last_name << '\n' ;
}


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.
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
// problem two
void User_Name( std::string& first_name, std::string* p_last_name /* may be nullptr */ )
{
    std::cout << "Enter first name: ";
    std::cin >> first_name;

    if( p_last_name != nullptr )
    {
        std::cout << "Enter last name: ";
        std::cin >> *p_last_name;
    }
}

int main()
{
    std::string first_name;
    std::string last_name;

    // we want both first and last names (pass non-null pointer)
    User_Name( first_name, std::addressof(last_name) ) ;
    std::cout << "\n\nUser name: " << first_name << ' ' << last_name << '\n' ;

    std::cout << "\n-----------------\n" ;

    // we want only the first names (pass nullptr)
    User_Name( first_name, nullptr ) ;
    std::cout << "\n\nUser first name: " << first_name << '\n' ;
}
JLBorges wrote:
it does so only if the caller passes in a NULL pointer for the last name.
Nope, see line 7. It ask only for the last name if not a null pointer is passed.

I think that the 'practice problem 2' is there for using both pointer and reference:
1
2
3
4
5
6
7
8
9
10
11
12
void User_Name( std::string& first_name, std::string*& p_last_name /* may be nullptr */ ) // Note: added reference &
{
    std::cout << "Enter first name: ";
    std::cin >> first_name;

    if( p_last_name == nullptr ) // Note: check that last name is null
    {
        p_last_name = new std::string{}; // Note: creating a new string
        std::cout << "Enter last name: ";
        std::cin >> *p_last_name;
    }
}
so when declaring the string variable " string last_name" it is NULL by default ? as in last_name = NULL ?
i get a messaging " error: 'nullptr' was not decalred in this scope" i'm using code::blocks
After std::string last_name;, the string is empty.
ie. last_name.empty() == true and last_name.size() == 0 ;

Only a pointer may be a null pointer; ie. it may not point to an object.

So:
1
2
3
4
5
6
7
    if( p_last_name != nullptr ) // if the pointer is not a null pointer (it points to a string)
    {
        std::cout << "Enter last name: ";
        std::cin >> *p_last_name; // enter the last name into the string that was pointed to
    }
    
    // else do nothing (nullptr; there is no string to write into) 
> error: 'nullptr' was not decalred in this scope"

Enable C++11. See: http://www.cplusplus.com/doc/tutorial/introduction/codeblocks/
In addition to what this tutorial asks you to do, also check -Wall and -pedantic-errors
this is what i'm trying to do, if p_last_name is nullptr ask user for last name.

#include <iostream>
#include <string>

using namespace std;

void User_Name (string *p_first_name, string *p_last_name )
{
cout << "Enter first name: ";
cin >> *p_first_name;
cout << endl;

if (p_last_name == nullptr){
cout << "Enter last name: ";
cin >> *p_last_name;
}


}

int main ()
{
string first_name;
string last_name;



User_Name(&first_name, std::addressof(last_name));

cout << endl << endl << "User name: " << first_name << " " << last_name;
}
> this is what i'm trying to do, if p_last_name is nullptr ask user for last name.

I think this is what you are expected to do: if p_last_name is not nullptr ask user for last name.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>

void User_Name( std::string* p_first_name, std::string* p_last_name /* both may be nullptr */ )
{
    if( p_first_name != nullptr ) // if the pointer p_first_name points to a string
    {
        std::cout << "Enter first name: "; // prompt for first name
        std::cin >> *p_first_name; // enter the first name into the string that was pointed to
    }

    if( p_last_name != nullptr ) // if the pointer p_last_name points to a string
    {
        std::cout << "Enter last name: "; // prompt for first name
        std::cin >> *p_last_name; // enter the last name into the string that was pointed to
    }
}

int main()
{
    std::string first_name;
    std::string last_name;

    // we want both first and last names (pass non-null pointer for both arguments)
    User_Name( std::addressof(first_name), std::addressof(last_name) ) ;
    // note: std::addressof(first_name) could also be written as &first_name
    // using std::addressof to get the address of an object a good habit to get into
    std::cout << "\n\nUser name: " << first_name << ' ' << last_name << '\n' ;


    std::cout << "\n-----------------\n" ;

    // we want only the first names (pass nullptr as the second argument)
    User_Name( std::addressof(first_name), nullptr ) ;
    std::cout << "\n\nUser first name: " << first_name << '\n' ;


    std::cout << "\n-----------------\n" ;

    // we want only the last names (pass nullptr as the first argument)
    User_Name( nullptr, std::addressof(last_name) ) ;
    std::cout << "\n\nUser last name: " << first_name << '\n' ;
}
thanks JLBorges
Topic archived. No new replies allowed.