【问题标题】:ERORR:warning comparison between pointer and integer错误:指针和整数之间的警告比较
【发布时间】:2016-02-01 09:52:38
【问题描述】:
#include <stdio.h>

int main()
{
    int user,pass;

    printf("New Username:\n");
    scanf("%d",&user);
    printf("New Password:\n");
    scanf("%d",&pass);
    printf("Type your username:\n");
    scanf("%d",&user);
    if ("%d"==&user)
    {
        printf("Username is good !!!!");
    }
    else
    {
        printf("Username is not good");
    }
    printf("type your password:\n ");
    scanf("%d",&pass);
    if ("%d"==pass)
    {
        printf("The password is good");
    }
    else
    {
        printf("The password is not good");
    }
    return 0;
}

请帮忙给我警告说:警告指针和整数之间的比较为什么?????? 怎么了??

指针和整数的警告比较 与字符串文字比较会导致未指定的行为

【问题讨论】:

  • if ("%d"==&amp;user),我是说,真的吗?
  • 它不好还是什么? :)
  • 这毫无意义——您正在将控制字符串与变量的地址进行比较——这没有任何意义。而且由于您将英语与某种语言(密码 noua?)混为一谈,因此很难提供帮助/
  • 你为什么笑……我是 C 新手……主要是我在 python 中工作……:)
  • 您确实需要检查从scanf() 返回的值。现在,您不知道您对scanf() 的任何调用是否实际读取数据。

标签: c compiler-errors


【解决方案1】:
if ("%d"==pass)

将字符串文字与整数进行比较?为什么?这是没有意义的。您的整数传递永远不能等于字符串文字"%d"(即{'%','d','\0'} btw)

那张支票完全是多余的。

if ("%d"==&user)

那更糟,这就是你的警告的来源。您正在将指向 int 的指针与字符串文字进行比较。

如果您想检查您的用户名和密码是否实际上是整数,则无需这样做,因为 scanf 会为您执行此操作。

【讨论】:

  • @KevinIT 没问题。今后调试前请不要提问。
【解决方案2】:

有两个错误。

if ("%d"==pass)

在这里,您将传递变量的值与字符串文字进行比较。这里是错误的,也不是必需的。

if ("%d"==&user)

在这里,您将user 变量的地址与字符串文字进行比较。这里是错误的,也不是必需的。


我已尝试将您的代码修改为某种级别。我想这就是你想要的。

是的,对于用户名,您需要使用 char array 来存储名称并使用 strcmp 来匹配它们。

#include <stdio.h>

int main()
{
    int user_org,pass_org;
    int user,pass; 

    // First store original username and password
    printf("Orignal Username:\n");
    scanf("%d",&user_org);
    printf("Original Password:\n");
    scanf("%d",&pass_org);


    // Check for username
    printf("Type your username:\n");
    scanf("%d",&user);
    if (user_org == user)
    {
        printf("Username match");
    }
    else
    {
        printf("Username is incorrect");
    }


    // check for password
    printf("type your password:\n ");
    scanf("%d",&pass);
    if (pass_org == pass)
    {
        printf("The password match");
    }
    else
    {
       printf("The password is not correct");
    }
    return 0;
}

【讨论】:

  • 我亲爱的选民请注意评论,我喜欢从自己的错误中吸取教训。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 2017-06-25
  • 1970-01-01
相关资源
最近更新 更多