首页 > 解决方案 > 如何修复错误“指针和整数之间的比较”?

问题描述

我一直在尝试比较结构变量和字符串变量。但我收到了这个错误。

#include<stdio.h>
#include<string.h>

int main()
{
    struct details {
        char number[20];
    } det[10];

    int inp, i=0;
    char aad; 
    int b;
    puts("Give The Number To Display The Pass Status");
    scanf("%s", &aad);

    for(b=0;b<i;b++)
    {
        if(det[i].number==aad)
        {
            printf("Hello");
        }
    }
    return 0;
}

请尝试修复我的错误

标签: c

解决方案


在您查看评论后,这将是答案

#include<stdio.h>
#include<string.h>

#define SIZE 20 // add size for aad

int main()
{

    struct details {
        char number[20];
    } det[10];

    int inp, i = 0;
    char aad[SIZE]; // must be array (can be with pointer, use malloc)
    int b;
    puts("Give The Number To Display The Pass Status");
    scanf("%s", &aad);

    for (b = 0; b < i; b++)
    {
        if (strcmp(det[b].number, aad) == 0) // strcmp to compare strings
        {
            printf("Hello");
        }
    }
    return 0;
}

推荐阅读