c - 获取错误 "passing argument 2 of ‘strcmp’ 使指针来自整数而不进行转换“比较两个字符串

标签 c arrays string matrix gcc-warning

我是一名学生,我正在努力完成这个计划,他的目标是:

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

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

警告:传递“strcmp”的参数 2 使指针来自整数而不进行强制转换

#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 进行比较,后者也是一个字符串。
我在这里错过了什么吗?

最佳答案

What im trying to do, is compare the string name[i] with searchname which is a string too.

char searchname; - 看起来不像字符串。 – 尤金·谢。

您可以尝试char searchname[100]; ... – 风向标

关于c - 获取错误 "passing argument 2 of ‘strcmp’ 使指针来自整数而不进行转换“比较两个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54657329/

相关文章:

c - 编译 C 程序时出错

arrays - Mongoose 用数组元素的条件填充嵌套数组

javascript - MVC .NET 将带有 Jquery 自动完成功能的 ViewBag 字符串 [] 数组传递给服务器 ActionResult

java - 如何在不创建任何中间字符串的情况下将 char[] 转换为 byte[]

将结构体中的字符数组与用户输入的字符数组进行比较

c - 是否存在 '&' 产生的性能低于 '%' 的情况?

c++ - 结构数组在调用时打印零,但在未调用时显示正确的输入(短代码)

c# - 哪个是在 ASP.NET(C#) 中使用字符串的最佳实践

java - 选择字符串的特定部分

c - git notes 迭代的例子