Having an hard time with char* arrays

Whatever I do with these crashes my stuff, I've been looking around on google and still no solutions, here's what I've got
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
char* frozenPlayers[32];
void AddFrozenPlayer(char* name){
	for (int i = 0; i < 32; i++){
		if (strcmp(frozenPlayers[i], name) == 0){ //already in the array, nothing to do

			frozenPlayers[i] = name;
			return;
		}
	}
}
void RemoveFrozenPlayer(char* name){
	for (int i = 0; i < 32; i++){
		if (strcmp(frozenPlayers[i], name) == 0){ //already in the array, nothing to do

			frozenPlayers[i] = NULL;
			return;
		}
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	
	AddFrozenPlayer("Player1");//Crashes on first call
	AddFrozenPlayer("Player2");
	RemoveFrozenPlayer("Player1");
	for (int i = 0; i < 32; i++){
		if (frozenPlayers[i] != NULL){
			if (strcmp(frozenPlayers[i], "Player1") == 0)
				cout << "Player1 is frozen" << endl;
			else if (strcmp(frozenPlayers[i], "Player2") == 0)
				cout << "Player2 is frozen" << endl;
		}
	}
	return 0;
}


Can anyone explain me how to do this properly?
Last edited on
I suggest you make a line at the beginning of your code that says something like: const size_t NUM_PLAYERS = 32; Then, everywhere you have a 32 in your code, replace it with NUM_PLAYERS. This way, you only have to change one line of code (the NUM_PLAYERS declaration) to change the number of players.

Your first line only declares that you have an array of pointers to chars. When you add a new name, you must allocate memory to stick the string of characters into.

First, initialize the array to get it into a known state (say, at the beginning of main):
1
2
3
4
    for (int i = 0; i < NUM_PLAYERS; i++)
    {
        frozenPlayers[i] = nullptr;
    }

In your AddFrozenPlayer code... describe your algorithm. What do you want to happen when you try to add a player? The code right now looks like it will write the new name into every slot in the array. I don't think this is what you want. If you describe more how your Add is supposed to work, I can help show you how to add a string properly.
Took a stab at what your Add logic may look like:
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <cstring>

using std::strcmp;
using std::strlen;
using std::cout;
using std::endl;

const size_t NUM_PLAYERS = 32;
char* frozenPlayers[NUM_PLAYERS];

//Gonna make this return bool so we can inform callers whether or not
//we actually added the name. True means we added the name, false means
//we didn't add it either because it was already there or we didn't have
//a spot in the array to put it.
bool AddFrozenPlayer(const char* name)
{
    //see if we already have this player
    for (int i = 0; i < NUM_PLAYERS; i++)
    {
        if (frozenPlayers[i] != nullptr && strcmp(frozenPlayers[i], name) == 0)
        {
            //if this spot in the array has data in it
            //AND that data matches the name we passed in
            //already in the array, nothing to do
            return false;
        }
    }

    //if we got this far then the name isn't in the array
    //look for the first empty spot and put the new name there
    for (int i = 0; i < NUM_PLAYERS; i++)
    {
        if (frozenPlayers[i] == nullptr) //if we found an empty spot in the array
        {
            frozenPlayers[i] = new char[strlen(name + 1)]; //allocate enough memory to hold a copy
            strcpy(frozenPlayers[i], name); //copy from the parameter into this newly allocated memory
            return true;
        }
    }

    //if we got this far, then we didn't add a name
    return false;
}

int main(int argc, char* argv[])
{
    //initialize the frozenPlayers array with nullptrs so we start from a known
    //state regarding the pointers in the array
    for (int i = 0; i < NUM_PLAYERS; i++)
    {
        frozenPlayers[i] = nullptr;
    }

    //add Player1
    if (AddFrozenPlayer("Player1"))
    {
        std::cout << "Successfully added Player1\n";
    }
    else
    {
        std::cout << "If you see this output, something went wrong!\n";
    }

    //try adding Player1 again
    if (AddFrozenPlayer("Player1"))
    {
        std::cout << "Shouldn't see this! We can't add the same player twice...\n";
    }
    else
    {
        std::cout << "Couldn't add the same player twice, just as expected!\n";
    }
}
Last edited on
Thanks for the help guys, works perfectly ! Wouldn't have figured it on my own ! That char* array was making me crazy...
Here's the remove logic. It is SO important that you clean up after yourself and reset the relevant pointer to null when removing a name.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Return value of true indicates a removal occured
//false indicates no removal (likely, name wasn't found)
bool RemoveFrozenPlayer(const char* name)
{
    for (int i = 0; i < NUM_PLAYERS; i++)
    {
        if (frozenPlayers[i] != nullptr && strcmp(frozenPlayers[i], name) == 0)
        {
            delete[] frozenPlayers[i];
            frozenPlayers[i] = nullptr;
            return true;
        }
    }

    return false;
}


And for good measure, since you initialized the array at the beginning of main, here's a way to clean up everything at the end of main:
1
2
3
4
5
6
7
8
9
10
11
12
    //clean up the frozenPlayers array
    //not a huge deal at the end of main since the OS will reclaim all memory
    // when the process ends, but it is good discipline to
    //clean up (i.e. delete and null out) pointers when you're done with them
    for (int i = 0; i < NUM_PLAYERS; i++)
    {
        if (frozenPlayers[i] != nullptr)
        {
            delete[] frozenPlayers[i];
            frozenPlayers[i] = nullptr;
        }
    }
Last edited on
Topic archived. No new replies allowed.