I used a predefined char variable in a case in my switch and got this error case label does not reduce to an integer
char player = 'X';
switch(.....){
case player:
.
.
.
.
I need a solution for this.
I used a predefined char variable in a case in my switch and got this error case label does not reduce to an integer
char player = 'X';
switch(.....){
case player:
.
.
.
.
I need a solution for this.
From the C11 standard:
The expression of each case label shall be an integer constant expression
player
is not a "constant expression".
Please note that in C qualifying a variable as const
, does not make it a "constant expression" in the sense of the C standard.
A label either need to be an integer literal, or a enum
, which in fact is an integer.
What you want is:
char player = 'X';
switch(player){
case 'X':
case 'Y':
case 'Z':
(A char is an encoding and the encoding is an int)