C语言 警告:指针和整数之间的比较[默认启用]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26145691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: comparison between pointer and integer [enabled by default]
提问by Yeez
So, I'm just practicing something in C language (I'm a beginner), but I'm now stuck on this program:
所以,我只是在用 C 语言练习一些东西(我是初学者),但我现在被困在这个程序上:
#include <stdio.h>
#include <string.h>
int main(){
int numbers[12] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
int i;
int *point;
point = numbers;
while(i){
printf("Write number in ranging 1 - 40: \n");
scanf("%d", &numbers);
if (numbers, point){
printf("Your number is prime number.\n");
}
else if ((numbers <= 0) || (numbers >= 41)){
printf("Only numbers: 1 - 40.\n");
}
else{
printf("Try again.\n");
}
i;
}
return 0;
}
When I want to compile it, I just get this error message:
当我想编译它时,我只是收到此错误消息:
test.c: In function ‘main':
test.c:19:38: warning: comparison between pointer and integer [enabled by default]
test.c:在函数“main”中:
test.c:19:38: 警告:指针和整数之间的比较 [默认启用]
I looked around Stack Overflow, but I found nothing of similar topic and didn't help me.
我环顾了 Stack Overflow,但没有找到类似的主题,也没有帮助我。
回答by David Grayson
Even if you're a beginner, you should still take the effort to tell us which lines are giving you errors, since it's not easy to tell exactly what line is number 19.
即使您是初学者,您仍然应该努力告诉我们哪些行给您带来了错误,因为要准确判断哪一行是第 19 行并不容易。
I think this line is giving you trouble.
我认为这条线给你带来了麻烦。
if (numbers, point){ // bad
You probably meant to call some function that takes numbersand pointas an argument. As it is right now, this if statement will always evaluate to true because you are using the comma operator, which just uses the value of the thing on its right, which happens to be a non-null pointer (point). How about something like:
您可能打算调用一些将numbers和point作为参数的函数。就像现在一样,这个 if 语句将始终评估为真,因为您使用的是逗号运算符,它只使用其右侧事物的值,恰好是一个非空指针 ( point)。怎么样:
if is_a_prime(numbers, point)
Moving on, the line that actually is causing the error is most likely this line:
继续,实际导致错误的行很可能是这一行:
else if ((numbers <= 0) || (numbers >= 41)){ // bad
Since numbersis an array, it doesn't make much sense to write numbers <= 0. Perhaps you meant to write numbers[0] <= 0, or *point <= 0, or maybe you wanted to use a forloop to iterate over each number in the array to make sure it is positive.
由于numbers是一个数组,所以写numbers <= 0. 也许您打算编写numbers[0] <= 0, or *point <= 0, 或者您想使用for循环来迭代数组中的每个数字以确保它是正数。
Also, (thanks to isedev for seeing this), you never set ito any value before accessing it, so your program will have undefined behavior. Try writing something like i = 1;near the top.
此外,(感谢 isedev 看到这一点),您i在访问它之前从未设置过任何值,因此您的程序将具有未定义的行为。尝试i = 1;在顶部附近写一些东西。
回答by Amr Ayman
First of all, this program will most likely not run. Specifically because you are saying while (i)when i is uninitialized (i.e garbage). It might be zero or any other value its initial address happens to point to.
首先,这个程序很可能不会运行。特别是因为您说while (i)i 未初始化(即垃圾)。它可能是零或它的初始地址恰好指向的任何其他值。
Secondly, what do you mean by if (numbers, point)? That doesn't mean anything.
Also, pointisnumbers, so there's no reason to compare them.
其次,你是什么意思if (numbers, point)?那不代表什么。
此外,pointisnumbers,所以没有理由比较它们。
Thirdly, if you want to check for a prime number use the modulo operator. Like this.
第三,如果您想检查素数,请使用模运算符。像这样。
Lastly, a pointer is notan integer or an array. When you say point = numbersthat just stores the address of numbersin memory into point, and using pointer arithmetic (and because pointer[3]and array[3]mean the same thing ~ pointer + (3 * sizeof(datatype))), you might think that a pointer is an array, but it's not. Here's an example:
最后,指针不是整数或数组。当您说point = numbers仅将numbers内存中的地址存储到 中point,并使用指针算术(因为pointer[3]和array[3]意味着相同的事情 ~ pointer + (3 * sizeof(datatype)))时,您可能会认为指针是一个数组,但它不是。下面是一个例子:
int h, *p;
h = 5;
*p = &h;
printf("*p: %d\n", *p);
h = 8;
printf("*p: %d\n", *p);
Here, *pis the value of h, and for a really simple reason.pstores the address of hin memory, and when you say h = 5the computer changes the value found at the address ofh in memory. And because phappens to store the address itself in it, it can simply get the value found there. In short, phas no relation to h, dot.
这里*p是 的值h,原因很简单。p将 的地址存储h在内存中,当您说h = 5计算机更改在内存中的地址h处找到的值时。并且因为p恰好将地址本身存储在其中,所以它可以简单地获取在那里找到的值。总之,p与h,点无关。
回答by Ginu Jacob
There are a couple of problems in this code:
这段代码有几个问题:
- Your
ivalue variable is uninitialized - If the garbage value of
iis non zero its an infinite loop - In the if condition you need to do a logical operation
- You are using
scanf()not with pointer you use are using pointer to pointer
- 您的
i值变量未初始化 - 如果 的垃圾值
i非零,则为无限循环 - 在 if 条件下你需要做一个逻辑运算
- 您使用的
scanf()不是指针,您使用的是指向指针的指针

