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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:25:30  来源:igfitidea点击:

Warning: comparison between pointer and integer [enabled by default]

c

提问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:

您可能打算调用一些将numberspoint作为参数的函数。就像现在一样,这个 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恰好将地址本身存储在其中,所以它可以简单地获取在那里找到的值。总之,ph,点无关。

回答by Ginu Jacob

There are a couple of problems in this code:

这段代码有几个问题:

  1. Your ivalue variable is uninitialized
  2. If the garbage value of iis non zero its an infinite loop
  3. In the if condition you need to do a logical operation
  4. You are using scanf()not with pointer you use are using pointer to pointer
  1. 您的i值变量未初始化
  2. 如果 的垃圾值i非零,则为无限循环
  3. 在 if 条件下你需要做一个逻辑运算
  4. 您使用的scanf()不是指针,您使用的是指向指针的指针