Pointing to a new array in a function.

So I have a function, given here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if(command == 'N')
	{
		char* firstname = new char[20];
		char* lastname = new char[20];
		char* login = new char[20];
		char* pass = new char[20];
		for (int i=0; i < 20; i++) 
		{
			firstname[i] = '\0';
			lastname[i] = '\0';
			login[i] = '\0';
			pass[i] = '\0';
		}
		int j = 2;
		int x = 0;
		//Code that assigns values to the arrays from the buffer.	
		addUser(firstname, lastname, login, pass);

Now, the idea is that addUser uses the pointers to the array to construct a new User object, which then has fields that point to the arrays. This function works great the first time it is called. The issue I run into is that the next time the function is called, Visual Studio tells me windows has triggered a breakpoint on the line where firstname is created. If I go and debug, it says something about a bad pointer.

Any help would be appreciated. I feel like there is something I am fundamentally misunderstanding.
Can you post you full code?

Meanwhile...
Do you want to (1)insert 20 names or (2)create an array with 20 elements.
I think it is 2. that is where the problem is. do: char firstname[20]; and not char* firstname = new char[20];. So your loop.?. must definitely
Okay, that seemed to solve that problem, but now I have a similar one later on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void RunningDB::addUser(char* first, char* last, char* login, char* pass)
{
	User user =  new User(first, last, login, pass);//Creates a new user.
	int i = 0;
	while(i < 20)//Runs through the array, determining if there is an empty spot.
	{
		if(_users[i] == NULL)//If there is, the newly created user is assigned to that index.
		{	
			_users[i] = user;
			break;
		}
		else i++;
	}
}

I run into a similar error here, but if I use a similar solution, as in:
User owner = User(first, last, login, pass);
I run into an error later in the program. RunningDB has the field
User* _users[20];
that the new User needs to be added to, but it doesn't like it not being a User*.
Topic archived. No new replies allowed.