Ismael
Ismael

Reputation: 197

Filling a char* in C

This is probably a very simple solution, but for the life of me I can't figure it out. I'm trying to create a char array (so a char*) consisting of numbers from 0 to numPlayers - 1, which I will iterate through to access whose turn it is. So, if numPlayers = 10, I want gameState.players to be [0,1,2,3,4,5,6,7,8,9]. What did I do wrong?

printf("How many players will be playing: ");
  scanf(" %d", &numPlayers);

  gameState.players = (char*) malloc(numPlayers * sizeof(char));

  for (int i = 0; i < numPlayers; ++i) {
    strcpy(gameState.players[i],(char) i);
  }

Upvotes: 0

Views: 906

Answers (2)

paxdiablo
paxdiablo

Reputation: 881403

First off:

gameState.players = (char*) malloc(numPlayers * sizeof(char));

The explicit cast is ill-advised in C (it can hide certain subtle errors) and the multiplication by sizeof(char) is never needed - it's always one.

But the real problem lies here:

strcpy(gameState.players[i],(char) i);

The str* functions are meant to work with C strings (null terminated character arrays). You do not have a string, rather you have a character value, so it should be more along the lines of:

gameState.players[i] = i;

You also need to keep in mind:

  • Though you're using char variables, the value being put in is not the textual representation of the digit. To get that, you would need to use i + '0'(a). Characters are generally meant to be used for (mostly) printable stuff, you would be better off using a more-specific data type like int or unsigned short` for non-character data.
  • This scheme (assuming you want textual representation) is going to break horribly if you ever use more than ten items.

(a) There's a big difference between the "characters" 7 and '7'. The former actually has the value 7 (ASCII BEL, if you're using ASCII), the latter has the value 0x37 (again, assuming ASCII/Unicode).

The numeric characters are the only ones guaranteed to be consecutive so you can convert a numeric value 0..9 to the printable character value simply by adding '0'.

Upvotes: 1

Pablo
Pablo

Reputation: 13580

Please be careful when calling functions, you have to make sure that you are using the correct types. If you use an incorrect one, the compiler will warn you or print an error, you should read the warnings and errors of the compiler. They tell you what is wrong.

strcpy is used to copy strings. The signature of the functions is

char *strcpy(char *dest, const char *src);

it expects a pointer to char as destination, and a pointer to char as the source. Also strcpy expects a valid C-String. A C-Strings is a sequence of char bytes that ends with the value '\0'. If you don't have a valid C-String, you cannot use strcpy.

strcpy(gameState.players[i],(char) i)

Is wrong on many levels:

  1. The arguments are not pointers of char
  2. You are not dealing with valid C-strings
  3. Casting won't help you here, you even did the wrong casting.
  4. 1 is not the same as '1'. The character '1' is actually the vakue 49 (ASCII code). If you want to get the ASCII representation of a digit, you have to do: '0' + digit.

You should do:

for (int i = 0; i < numPlayers; ++i) {
    gameState.players[i] = '0' + i;
}

Note that this would only work for max. 9 players. If you need more, then you have to use an array of strings.

Also note that gameState.players does not point to a C-String, because it is not '\0'-terminated. You cannot calls strings functions on it. If you want to do that, then you have to change your code like this:

printf("How many players will be playing: ");
fflush(stdout);
scanf(" %d", &numPlayers);

if(numPlayers > 9)
{
    // error, do not continue
}

gameState.players = calloc(numPlayers + 1, sizeof *gameState.players);

for (int i = 0; i < numPlayers; ++i) {
    gameState.players[i] = '0' + i;
}

Upvotes: 0

Related Questions