Answer the question
In order to leave comments, you need to log in
How to assign a char character to an integer element of an array?
I have a 3x3 matrix of type int. How to assign an element to any of this matrix any character? I took the second char array to at least see the result.
// у arrayI значения элементов от 1 до 9
void slct(int arrayI[3][3], char arrayC[3][3]) {
int x = getchar() - '0';
memset(arrayC, '-', sizeof arrayC);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if( x == arrayI[i][j]) {
arrayC[i][j] = 'W';
}
printf("[ %d ]", arrayC[i][j]);
}
printf("\n");
}
}
/* Вывод такой:
Вводим 5 и на 5, т.е. 4 элементе 88, как сделать 'W'?
[ 0 ][ 0 ][ 0 ]
[ 0 ][ 88 ][ 0 ]
[ 0 ][ 0 ][ -115 ]
Почему 8 элемент равен чему-то? Как это исправить?
*/
Answer the question
In order to leave comments, you need to log in
memset(arrayC, '-', sizeof arrayC);
arrayC[i][j] = 'W';
In C/C++, the char type is a 1-byte signed integer. Therefore, intu is assigned directly, without any dancing:
arayI[i][j] = arrayC[i][j]
To print a character instead of a number, use the template %c in printf.
It looks like your code doesn't match the output you provide in the comments below.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question