[英]Issues with char and string assignments on C (integer from pointer without a cast?)
I've made the following declarations on my code:我在我的代码上做了以下声明:
char bussola, com[1], pen;
int main()
{
bussola = "oeste", pen = "up";
but for some reason I'm getting this error on the compiler:但由于某种原因,我在编译器上收到此错误:
main.c:18:10: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
bussola = "oeste", pen = "up";
^ ^
And because of that I keep getting other errors like this one:正因为如此,我不断收到类似这样的其他错误:
main.c:51:17: warning: comparison between pointer and integer
if (bussola == "oeste")
^~
What should I do?我应该怎么办?
you are assigning a char array (AKA string) to a char variable, they are't compatible data tipes.您正在将一个字符数组(AKA 字符串)分配给一个字符变量,它们不是兼容的数据类型。 A char variable can only have single char values (like 'a' , 'F', '2' or ' ').
char 变量只能有单个 char 值(如 'a' 、 'F' 、 '2' 或 ' ')。 Its important to use single quotes (' ') to interpret the character like a char variable.
使用单引号 (' ') 将字符解释为 char 变量很重要。 You can declare a string using char * bussola or char bussola[LENGTH], but you will have to inicialite the array in the declaration:
您可以使用 char * bussola 或 char bussola[LENGTH] 声明一个字符串,但您必须在声明中初始化数组:
char bussola[6] = "oeste";
if you want to inicialite it later, you can use strcpy:如果你想稍后初始化它,你可以使用 strcpy:
char bussola[6];
strncpy(bussola, "oeste", 6);
Be aware that string use doble quotes (" ") instead of single quotes like chars.请注意,字符串使用双引号 (" ") 而不是像字符那样的单引号。
char bussola[6], pen[3];
int main() {
strcpy(bussola, "oeste");
strcpy(pen, "up");
}
if(!strcmp(bussola,"oeste")
This above is the correct syntax for what you are trying to do.以上是您尝试执行的操作的正确语法。
Considering this code, I would suggest to thorough the strings and character concepts in C and also the string.h
header file functions.考虑到这段代码,我建议彻底了解 C 中的字符串和字符概念以及
string.h
头文件函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.