Probably simple but confusing assignment error

老子叫甜甜 提交于 2019-12-13 05:19:32

问题


#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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!