得到错误“传递'strcmp'的参数2使得指针来自整数而没有强制转换”比较两个字符串

问题描述 投票:0回答:1

我是学生,我试图完成这个项目,他的目标是:

在输入中打印与请求的名称同名的产品数据

每次,当我尝试编译程序时,会出现以下警告:

warning: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast

#include <stdio.h>
#include <string.h>
#define N 100
#define M 8

int main (void) {
    int n;
    char name[N][M];
    char code[N][M];
    int price[N];
    char searchname;
    int i;
    int selection;
    int search, x;


    do {
        printf("Insert how many products to register: ");
        scanf("%d", &n);
    } while(n > N);

    for(i = 0; i < n; i++) {
        printf("Insert the name of the product n%d: ", i + 1);
        scanf("%s", name[i]);

        printf("Insert the code of the product registered: ");
        scanf("%s", code[i]);

        printf("Insert the price of the product registered: ");
        scanf("%d", &price[i]);
    }

    do {
        printf("Choose one of the following options:\n\n");
        printf("1) Print name and price of the searched product (code)\n");
        printf("2) Print the product that has the same name as the name inserted\n");
        printf("0) Close the program\n\n");
        printf("Type the number of the option: ");
        scanf("%d", &selection);

        switch(selezione) {
            case 1:
                x = 0;

                printf("Insert the code of the product: ");
                scanf("%d", &search);

                for(i = 0; i < n; i++) {
                    if(code[i] == search) {
                        printf("name: %s | price: %d\n\n", name[i], price[i]);

                        x = 1;
                    }
                }

                if(x == 0) {
                    printf("The searched code doesnt exist\n\n");
                }

            break;
            case 2:
                x = 0;

                printf("Insert the name to search: ");
                scanf("%s", searchname);

                for(i = 0; i < n; i++) {
                    while(strcmp(name[i], searchname) == 0) {
                        printf("Product number %d | Code: %s | Price: %d\n\n", i + 1, code[i], price[i]);
                        x++;
                    }
                }

                if(x == 0) {
                    printf("There are no products with this name\n\n");
                }

            break;
            case 0:
            break;
            default:
                printf("This option doesnt exist");
            break;
        }
    } while(selection!= 0);

    return 0;
}

我搜索堆栈溢出了一段时间,提到这个问题的帖子没有帮助。 我试图做的是比较字符串name[i]searchname这也是一个字符串。 我错过了什么吗?

c arrays string matrix gcc-warning
1个回答
0
投票

我试图做的是比较字符串name[i]searchname这也是一个字符串。

char searchname; - 看起来不像字符串。 - 尤金Sh。

你可以试试char searchname[100]; ...... - 风向标

© www.soinside.com 2019 - 2024. All rights reserved.