在if语句中使用的C函数中使用数组类型错误的表达式分配



,所以我试图制作一个类似选择的代码,在其中您输入某些内容以获取命令以运行并输入其他命令,我尝试使用由于我试图学习和弄清楚如何使用它,因此可以通过void命令进行功能但是我仍在学习soooo(

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char commandA[20];    
    char commandB[20];
    char click [20];
    scanf("%s",click);
    if (click=commandA){
        command1();
    } else if (click=commandB){
        command2();
    }
}
void command1(){
    printf("i don't know what to type here ");
}
void command2(){
    printf("i don't know what to type here x2");
}
}

我希望能够键入Commanda并获取第一个printf消息,并且我希望能够键入命令b获取第二个printf消息,这是我收到的其他警告和错误:

|11|error: assignment to expression with array type|
|12|error: assignment to expression with array type|
|11|warning: implicit declaration of function 'command1' [-Wimplicit-function-declaration]|
|12|warning: implicit declaration of function 'command2' [-Wimplicit-function-declaration]|
|14|warning: conflicting types for 'command1'|
|16|warning: conflicting types for 'command2'|

第一个错误是因为您在if语句中使用=而不是===用于分配,==用于比较平等。但是,为了比较字符串,您必须使用strcmp()功能;如果您使用==,则只需比较数组的地址,而不是内容。

关于隐式声明的错误是因为您将command1command2的定义放在main()之后。C需要在使用之前定义或声明函数,因此您要么必须向下移动main(),要么将功能原型放在其之前。

您还需要初始化commandAcommandB

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void command1(){
    printf("i don't know what to type here ");
}
void command2(){
    printf("i don't know what to type here x2");
}
int main()
{
    char commandA[20] = "cmdA";    
    char commandB[20] = "cmdB";
    char click [20];
    scanf("%s",click);
    if (strcmp(click, commandA) == 0){
        command1();
    } else if (strcmp(click, commandB) == 0){
        command2();
    }
}

相关内容

最新更新