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