简体   繁体   English

赋值使指针从整数变成整数,而不进行强制转换[-Wint-conversion]

[英]assignment makes integer from pointer without a cast [-Wint-conversion]

i am new to c, i am trying to make a simple code in which i replace the vowels of an input with the * character... i am having trouble with the array (i am mainly familiar with python only using c now.) 我是c语言的新手,我正在尝试编写一个简单的代码,在其中我用*字符替换输入的元音...我在数组上遇到了麻烦(我现在主要只使用c来熟悉python)。

#include <stdio.h>

 char* main() {
 int c;
 char* vowels[] = {'a', 'e', 'i', 'o', 'u'};

 while( 1 ) {   // means: true
  c = getchar();
  if(c == EOF) break;
  if(c = vowels) putchar('*'); 
  else putchar(c);
}
}

i keep receiving the same error message: novowels.c:5:41: note: (near initialization for 'vowels[4]') novowels.c:10:12: warning: assignment makes integer from pointer without a cast [-Wint-conversion] if(c = vowels) putchar('*'); 我一直收到相同的错误消息:novowels.c:5:41:注意:(在'vowels [4]'的初始化附近)novowels.c:10:12:警告:赋值使指针从整数转换而没有强制转换[-Wint -conversion] if(c =元音)putchar('*'); ^ ^

.... please help, i can't quite figure out how do complete this program of replacing vowels with * ....请帮助,我不太清楚如何完成用*替换元音的程序

Firstly, char *main() is illegal in C. It should be int main() . 首先, char *main()在C中是非法的。它应该是int main()

Secondly, 'a' and the like are integer constants in C. You cannot use them to initialize and array of char * elements. 其次, 'a'等是C中的整数常量。您不能使用它们来初始化char *元素和数组。 If you wanted a char array you should have declared it as 如果您想要一个char数组,则应将其声明为

const char vowels[] = {'a', 'e', 'i', 'o', 'u'};

If you wanted a string array you should have declared it as 如果您想要一个字符串数组,则应将其声明为

const char *const vowels[] = {"a", "e", "i", "o", "u"};

Finally, it is completely unclear what you were trying to say by c = vowels . 最后,用c = vowels试图说什么完全不清楚。 If you want to check whether c is in vowels array, keep in mind that there's no core language feature that can do it for you (and certainly not the = operator). 如果要检查c是否在vowels数组中,请记住,没有核心语言功能可以为您做这件事(当然不是=运算符)。 You have to implement the check manually or use an applicable library feature. 您必须手动实施检查或使用适用的库功能。

For example, one possible way to check it is 例如,一种可能的检查方法是

const char vowels[] = {'a', 'e', 'i', 'o', 'u'};
...
if (memchr(vowels, c, sizeof vowels) != NULL)
  putchar('*');

Alternatively, it can be expressed as 或者,它可以表示为

const char *const vowels = "aeiou";
...
if (strchr(vowels, c) != NULL)
  putchar('*');

First of all, your program contains a typo. 首先,您的程序包含一个错字。 == is comparison whereas = is assignment. ==是比较,而=是赋值。 Instead of this line 代替这条线

if(c = vowels) putchar('*');

you probably meant to write 你可能想写

if(c == vowels) putchar('*');

but even that is incorrect. 但这甚至是不正确的。 You cannot use the == operator to test for membership. 您不能使用==运算符测试成员资格。 To test for membership, you could use the strchr() function from <string.h> : 要测试成员资格,可以使用<string.h>strchr()函数:

if (memchr(vowels, c, sizeof vowels) != NULL) putchar('*');

You can read here about what memchr does. 您可以在这里阅读有关memchr功能。

Lastly, as AnT already remarked, main shouldn't return a char* . 最后,正如AnT已经指出的那样, main不应返回char* The main function must return an int in a strictly conforming C program. main函数必须在严格符合要求的C程序中返回int Change the line 换线

char* main() {

to

int main() {

to fix this. 解决这个问题。

The problem is that you are not comparing anything in c = vowels . 问题是您没有比较c = vowels任何内容。 You are assigning to c the address of the array vowels (which is a pointer indeed). 您正在向c分配数组vowels的地址(实际上是一个指针)。

Probably you wanted to check equality between c and one of the vowels but you must use the [] operator since vowels is an array, an index, and a different operator (comparison, which is == ). 可能您想检查c和一个元音之间的相等性,但是您必须使用[]运算符,因为vowels是一个数组,一个索引和一个不同的运算符(比较, == )。 Something like: 就像是:

if (c == vowels[2])

but then vowels should be a char[] not a char*[] . 但是元音应该是char[]而不是char*[]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 &#39;char *&#39; 对 &#39;char&#39; 的赋值使指针从没有强制转换的整数 [-Wint-conversion] - assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion] 为什么这是“从‘int’到‘char *’的赋值使来自 integer 的指针没有转换 [-Wint-conversion]” - Why this is "assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]" 警告:赋值使 integer 从没有强制转换的指针 [-Wint-conversion] 在 C - warning: assignment makes integer from pointer without a cast [-Wint-conversion] in C 警告:赋值使指针从整数变成整数,而不进行强制转换[-Wint-conversion] - Warning: assignment makes integer from pointer without a cast [-Wint-conversion] 警告:赋值使指针从整数开始而没有强制转换[-Wint-conversion] - warning: assignment makes pointer from integer without a cast [-Wint-conversion] 警告:赋值使指针来自 integer 没有强制转换 [-Wint-conversion] p = (i + 1) * 100; - warning: assignment makes pointer from integer without a cast [-Wint-conversion] p = (i + 1) * 100; 赋值使 integer 从没有强制转换的指针 [-Wint-conversion] 在 C - Assignment makes integer from pointer without a cast [-Wint-conversion] in C 警告:赋值使指针从整数开始,而在C程序中没有强制转换[-Wint-conversion] - warning: assignment makes pointer from integer without a cast [-Wint-conversion] in C program 如何解决“赋值使指针从整数开始而无需强制转换[-Wint-conversion]”? UNIX程序 - How to fix “ assignment makes pointer from integer without a cast [-Wint-conversion]”? C unix program 使用 function 指针,但得到警告使来自 integer 的指针没有强制转换 [-Wint-conversion] - Using function pointer, but got warning makes pointer from integer without a cast [-Wint-conversion]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM