Invalid Null Pointer

Hello Guys,

First of all, I'm glad to meet you in this forum.

I wrote a code to input name in while loop and print the content of character pointer array. but after finishing the compile, I got the error message: "invalid null pointer!"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  int main(void)
{
	char* name[30];	
	char* temp = NULL;
	int len = 0;

	while (true)
	{
		cout << "Name:";
		cin.getline(temp, sizeof(temp));
		if (cin.eof())
			break;
		name[len++] = temp;
	}

	for (int i = 0; i < len; i++)
		cout << name[i] << endl;

	system("pause");
	return 0;
}

I want to figure out what's wrong in my code but I can' do it.
I need help...

thx,

c00012
Look at the following snippet:

1
2
3
	char* temp = NULL;
...
		cin.getline(temp, sizeof(temp));


Where have you allocated memory for this pointer?

Since this is C++ you really should be using std::string instead of the C-strings, then you won't need to worry about memory allocation. And don't forget to insure that you never exceed the size of your name[] array in that loop.
change char* temp = NULL to char temp[30]

refer to: http://www.cplusplus.com/reference/istream/istream/getline/
Last edited on
Thanks for all suggestion. I'll do as you guys suggested and I'll let you know if i resolve the problem.
Topic archived. No new replies allowed.