期望的const char *但是参数是char类型

时间:2014-12-01 05:24:06

标签: c pointers struct strcmp strcpy

此错误不断弹出,我不知道如何解决 请帮忙!此行中弹出错误:
---fscanf(ifp, "%s", archive.team[i].color);---

行中还有"passing argument 2 of strcmp makes pointer from integer without a cast"个错误:
---if (strcmp(archive.team[j].name, name) == 0){---

以下是我的代码的精简版--------------------------------------- -------------------------------------------------- -------------------------------------------------- ---------------------------------------------

#include <stdio.h>

struct dragon {
    char name[40] ;
    char color[40] ;
};
struct collection {
    struct dragon team[1000];
    int num_dragons;
};

int main() {

struct collection archive;

FILE * ifp = fopen("dragon.txt", "r");
int i, j, k, l, m, updates, n=0;
char directions, color, name, ADD, REMOVE, SEARCH, LIST, rem;

fscanf(ifp, "%d", &updates);
for (i=0; i<updates; i++){
    fscanf(ifp, "%s", directions);

    if (directions==ADD){
        fscanf(ifp, "%s", archive.team[i].name);
        fscanf(ifp, "%s", archive.team[i].color);
        printf("%s the %s has been added to the team.", archive.team[i].name, archive.team[i].color);
    }

   else if (directions==REMOVE){
        fscanf(ifp, "%s", name);
        for (j=0; j<updates; j++){
            if (strcmp(archive.team[j].name, name) == 0){
                strcpy(archive.team[j].name, "rem");
                strcpy(archive.team[j].color, "rem");
                printf("%s the %s has been removed from the team.", name, archive.team[j].color);
            }
        }
    }

    else if (directions==SEARCH){
        fscanf(ifp, "%s", name);
        for (k=0; k<updates; k++){
            if (strcmp(archive.team[k].name, name) == 0)
            printf("%s the dragon is currently on the team.", name);
        }
        for (l=0; l<updates; l++){
            if (strcmp(archive.team[l].name, name) == 0)
                n++;
        }
        if (n==0)
            printf("%s the dragon is NOT currently on the team.", name);

    }

    else if (directions==LIST){
        fscanf(ifp, "%s", color);
        printf("%s dragons:\n", color);
        for (m=0; m<updates; m++){
            if (strcmp(archive.team[m].color, color) == 0)
            printf("%s\n", archive.team[m].name);
        }
    }
}

3 个答案:

答案 0 :(得分:2)

您的声明:

char directions, color, name, ADD, REMOVE, SEARCH, LIST, rem;

完全错了。您尝试将字符串写入单个char。即使您设法编译它,执行此类代码也会导致缓冲区溢出和意外行为(AV最有可能)。

比较

if (directions==ADD)

也很可疑,你将一些数据与未初始化的char进行比较,这显然不会起作用。 使用固定长度数组也不是一个好主意,因为任何大于39个符号的字符串都会导致缓冲区溢出。

老实说,建议您使用std :: string作为字符串存储来重写代码,使用std :: cin / cout作为输入/输出。

让我们从头开始分析你的代码:你定义变量方向,它应该描述应该对你的数据执行什么操作。这个变量是一个char,但你试着写一些sting,所以你很可能会在这里得到AV。然后,您尝试将数据与一些从未初始化的ADD变量进行比较,因此包含堆栈中的随机字符。

使当前解决方案可行: 1.将方向声明更改为char[40]; 2.将ADD更改为char* ADD = "ADD";(REMOVE,SEARCH等应以相同方式更改); 3.将比较代码更改为if(!strcmp(directions, ADD))

答案 1 :(得分:1)

我认为这些是给出错误的行

 fscanf(ifp, "%s", name);
 fscanf(ifp, "%s", directions);

而不是

 fscanf(ifp, "%s", archive.team[i].color);

,因为

 name is of type `char` and not `char []`.

再次,这里

 strcmp(archive.team[j].name, name)

第二个参数应为char []类型,但类型为char

问题出在这里

char directions, color, name, ADD, REMOVE, SEARCH, LIST, rem;

colorname可能需要是数组,例如。

char directions, color[40], name[40], ADD, REMOVE, SEARCH, LIST, rem;

答案 2 :(得分:0)

我想,你忘了关闭the main function properly