可能简单但令人困惑的分配错误 [英] Probably simple but confusing assignment error
本文介绍了可能简单但令人困惑的分配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#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
推荐答案
您必须分配char
而不是pointer to a string literal
.使用'
代替"
You have to assign a char
not a pointer to a string literal
. Use '
instead of "
gradesList[2] = 'X';
在C中,字符串文字使用双qoutes表示,即
"
.并且char
用单引号表示,即'
.
In C string literals are represented using double qoutes, i.e.
"
. And char
are represented using single quotes, i.e. '
.
由于已将gradesList
声明为char
数组.并尝试将指针分配给字符串文字,您会收到此错误.
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.
这篇关于可能简单但令人困惑的分配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文