简体   繁体   English

C 上的字符和字符串赋值问题(来自指针的整数,没有强制转换?)

[英]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.

相关问题 “来自整数的指针/来自没有强制转换的指针的整数”问题 - "Pointer from integer/integer from pointer without a cast" issues 从 'char *' 初始化 'char' 从指针生成整数而不进行强制转换 - initialization of ‘char’ from ‘char *’ makes integer from pointer without a cast 指针中没有C转换的整数 - integer from pointer without a cast in C c-整数指针,无强制转换 - c- pointer from integer without cast C 多维字符数组 - 赋值从指针生成整数而不进行强制转换 - C multidimentional char array - assignment makes integer from pointer without a cast 请解决此错误'从'char *'初始化'char'使integer从没有强制转换的指针'5 | 字符星 = "*"; - Please resolve this error ' initialization of ‘char’ from ‘char *’ makes integer from pointer without a cast ' 5 | char star = "*"; 没有强制转换的整数指针 - Pointer from integer without a cast 从 'char *' 对 'char' 的赋值使指针从没有强制转换的整数 [-Wint-conversion] - assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion] 如何修复 - 从 'char *' 分配给 'char' 使得 integer 从没有强制转换的指针 - How to fix - assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast 从'const char *'分配给'char'使得integer从指针没有强制转换 - assignment to 'char' from 'const char *' makes integer from pointer without a cast
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM