Passing Null Pointer to a Function

Write your question here.
How do you pass a NULL pointer to a function?

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
  /*
*	Chapter 13 Problem 3
*
*	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.
*
*
*
*/

#include <iostream>
#include <string>

using namespace std;

//	Function Prototype
void Name(string* first, string* last);


int main()
{
	string firstName;
	string lastName = NULL;
	Name(&firstName, &lastName);
	cout << firstName << " " << lastName << " is my name." << endl;
}


void Name(string* first, string* last)
{
	cout << "Please enter your first name: ";
	cin >> *first;
	
	if (last == NULL)
	{
		cout << "Please enter your last name: ";
		cin >> *last;
	}
}
1
2
	string lastName = NULL;
	Name(&firstName, NULL);


[EDIT]
Note that the prompt is supposed to appear if the parameter is NULL.
Last edited on
That task makes no sense. Line 35 is true if last does not point to a string and in that case you will attempt to dereference a null on line 38 and store input to a string that does not exist. Error, crash.


"Ask name only if not null" would make sense.


Anyway, Name(&firstName, 0);
Pass NULL:
Name( &first, NULL );

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main( void )
{
  int number = 1234;
  int *pointer = &number;

  std::cout << "Value of number:     " << number   << '\n';
  std::cout << "Address of number:   " << &number  << '\n';
  std::cout << "Value of pointer:    " << pointer  << '\n';
  std::cout << "Address of pointer:  " << &pointer << '\n';
  std::cout << "Dereference pointer: " << *pointer << '\n';

  return 0;
}
Passing NULL is of no use, the user still wants to get the lastName out.

passing a parameter which is a string* is no good either because it needs to be modifiable so that the callers pointer can be updated with a newly allocated one from name().

so I think the lastName really needs to be a string** so that it can be allocated and assigned from within name() and the pointer can be assigned to the callers via *lastName = theNewStringPtr.

really though, i think the idea of passing NULL for a string is a bit oldskool, it would be much cleaner with an empty string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
	string firstName;
	string* lastName = NULL;
	Name(&firstName, &lastName);
	cout << firstName << " " << *lastName << " is my name." << endl;
}


void Name(string* first, string** last)
{
	cout << "Please enter your first name: ";
	cin >> *first;
	
	if (*last == NULL)
	{
               *last = new string;
		cout << "Please enter your last name: ";
		cin >> **last;
	}
}


Jaybob66,

You got it correctly. I want to get the last name out.

However, I have not yet learned what "new" and "**" are. So, it is not the solution I am looking for.

Your program works though. Eventually, I will get to know what these things are.

Thank you.
You want a name, if you don't provide a place to store it in, but
you don't want a name, when you do give a place to store it in?

I would do exactly the opposite.


"**" is a pointer to a pointer. One could use a reference to a pointer as well. That would be slightly more explicit.
However, I have not yet learned what "new" and "**" are. So, it is not the solution I am looking for.


I'm sorry to say you will have to use 'new' because if the caller passes in NULL then there is nowhere to store lastName so you will have to explicitly allocate it with new. You cant use a local variable within name() because it will deallocate when the function returns.

You will also have to use either ** or &* as keskiverto says, either way you need to access the memory outside of the function name() and because you are trying to give it a new pointer (from your allocation) you will need the address of that pointer, hence ** or &*.

He could return a value from the function.
true, but that would also mean calling the routine differently depending on which parameters he had available.
I would add default values into the mix:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void foo( int * a, int * b = 0 )
{
  *a = 7;
  if ( b ) *b = 42;
}


// use
int x, y, z;

// needs both
foo( &x, &y );

// needs first
foo( &z );

This, obviously, is not the answer to "Chapter 13 Problem 3".
LowestOne,

Thank you for providing the example code. Very helpful in understanding pointers and addresses.

Jaybob66,

You nailed it again. I am now on Chapter 14, and it covers "new" and "**". I understand what you've done. Your explanations are excellent.

keskiveto,

Thank you for exchange between you and Jaybob66. Right now though you are soaring way above me.
Topic archived. No new replies allowed.