Reputation: 822
I'm trying to build a basic text chess game. Currently in every array position, i'm trying to enter a 2 character word. P1 is first player's pawn. Its giving me incompatible pointer to integer conversion
error on the strcpy
function line.
I tried to use ChessArray[firstCounter][secCounter] = 'P1';
but it did not work and give me an error.
NOTE: I know a lot of people will say its better to use another language to make this game but i wanted to practice my C skills.
code:
#include <stdio.h>
#include <string.h>
int main(int argc, const char * argv[]) {
char ChessArray[8][8];
for (int firstCounter = 0; firstCounter <= 7; firstCounter++)
{
for (int secCounter = 0; secCounter <= 7; secCounter++)
{
strcpy(ChessArray[firstCounter][secCounter],"p");
}
}
printf(" |A |B |C |D |E |F |G |H |\n");
for (int firstCounters = 0; firstCounters <= 7; firstCounters++)
{
printf("%i", firstCounters);
for (int secCounters = 0; secCounters <= 7; secCounters++)
{
printf("| ");
printf("%c",ChessArray[firstCounters][secCounters]);
}
printf("|");
printf("%i",firstCounters);
printf("\n");
}
printf(" |A |B |C |D |E |F |G |H |\n");
return 0;
}
Upvotes: 0
Views: 427
Reputation: 93559
You're confusing a char* and char. Your ChessArray is an array of array of characters. Not an array of array of char*. A string in double quotes is a char*. A single character in single quotes is a character. Strcpy works with char*, not with characters, and it can't copy data into a character. Either you want to make it a char *ChessArray[][], or you want to use ChessArray[firstCounter][secCounter] = 'p'.
Note that if you use the char* ChessArray[][] version you have to allocate the memory each of those char* at each of those indices holds with malloc before you pass it to strcpy the first time.
Upvotes: 4