Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
应为常量char*,但参数的类型为char_C_Pointers_Struct_Strcmp_Strcpy - Fatal编程技术网

应为常量char*,但参数的类型为char

应为常量char*,但参数的类型为char,c,pointers,struct,strcmp,strcpy,C,Pointers,Struct,Strcmp,Strcpy,这个错误不断出现,我不知道如何解决它。 请帮忙!此行中会弹出错误: --fscanf(ifp,“%s”,archive.team[i].color)--- 还有一个“strcmp的传递参数2使指针从整数变为非强制转换”行中出现错误: --if(strcmp(archive.team[j].name,name)==0){-- 这是我的代码的浓缩版本----------------------------------------------------------------------------

这个错误不断出现,我不知道如何解决它。
请帮忙!此行中会弹出错误:
--fscanf(ifp,“%s”,archive.team[i].color)---

还有一个
“strcmp的传递参数2使指针从整数变为非强制转换”

行中出现错误:
--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);
        }
    }
}
#包括
结构龙{
字符名[40];
炭色[40];
};
结构集合{
结构龙团队[1000];
int num_龙;
};
int main(){
结构收集档案;
文件*ifp=fopen(“dragon.txt”、“r”);
int i,j,k,l,m,更新,n=0;
字符方向、颜色、名称、添加、删除、搜索、列表、rem;
fscanf(ifp、%d、&更新);

对于(i=0;i我认为这些线给出了误差

 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;
color
name
可能需要是数组,例如

char directions, color[40], name[40], ADD, REMOVE, SEARCH, LIST, rem;
你的声明:

char directions, color, name, ADD, REMOVE, SEARCH, LIST, rem;
完全错误。您尝试将字符串写入单个字符。即使您成功编译了该字符串,执行此类代码也会导致缓冲区溢出和意外行为(极有可能是AV)

比较

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

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

让我们从一开始就分析您的代码:您定义变量方向,它应该描述应该对您的数据执行的操作。此变量是一个字符,但您尝试在其中写入一些sting,因此您很可能会在此处获得AV。然后您尝试将您的数据与一些从未初始化过的ADD变量进行比较,因此包含从堆栈中删除随机字符

要使当前解决方案可行,请执行以下操作: 1.将方向声明更改为
字符[40]
; 2.将添加更改为
char*ADD=“ADD”
(删除、搜索等应以相同方式更改);
3.将比较代码更改为
如果(!strcmp(directions,ADD))
我猜您也忘记关闭
存档声明了

?请编写一个完整的可验证代码“压缩版本”如果你漏掉了重要的部分,而在这里输入代码这样的废话是没有用的,
字符…名称
-这就是你的问题,就像错误所说的那样。当你需要一个
字符*
,或者一个
字符数组时,这是一个
字符
。这同样适用于
颜色
。你已经准备好了ur
struct
,你只需要在它之外做同样的事情。得到一个简明的答案:错误在你跳过的部分。我想那可能是它。关于如何在语法上修复它,有什么建议吗?@DavidT-为
color
name
添加了正确的声明。我试过了。它去掉了e错误,但它在编译到屏幕时崩溃。我猜可能是其他错误造成的?啊,我很抱歉。我是一个初学者程序员,这是我一直坚持的任务之一。我如何重写我的字符声明以使其编译,以及如何初始化未初始化的字符?老实说,问题远比字符d更深让我们从一开始就分析你的代码:这可能是一个有用的链接,但它仍然是注释。