问题
#include <stdio.h>
int main() {
char gradesList[5];
gradesList[2] = "X";
printf("%c", gradesList[2]);
}
When I try to run this code I get these errors:
Incompatible pointer to integer conversion
Assignment makes integer from pointer without a cast
回答1:
You have to assign a char
not a pointer to a string literal
. Use '
instead of "
gradesList[2] = 'X';
In C string literals are represented using double qoutes, i.e.
"
. And char
are represented using single quotes, i.e. '
.
Since you have declared gradesList
as a char
array. And are trying to assign a pointer to a string literal, you are getting this error.
回答2:
Anything inside double quote is considered as string.
you should use :
gradesList[2] = 'X';
来源:https://stackoverflow.com/questions/33603276/probably-simple-but-confusing-assignment-error