在 if 语句中使用的 C 函数中对具有数组类型错误的表达式进行赋值
assignment to expression with array type error in C functions used in if statements
所以我试图做一个类似选择的代码,你输入一些东西来获取 运行 的命令,然后你输入其他东西来 运行 另一个命令,我试着这样做这与 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 消息,我希望能够键入 commandB 以获得第二条 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()
函数;如果你使用 ==
,它只是比较数组的地址,而不是内容。
关于隐式声明的错误是因为你把command1
和command2
的定义放在了main()
之后。 C 要求在使用函数之前定义或声明函数,因此您要么必须向下移动 main()
,要么将函数原型放在它之前。
您还需要初始化 commandA
和 commandB
。
#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();
}
}
所以我试图做一个类似选择的代码,你输入一些东西来获取 运行 的命令,然后你输入其他东西来 运行 另一个命令,我试着这样做这与 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 消息,我希望能够键入 commandB 以获得第二条 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()
函数;如果你使用 ==
,它只是比较数组的地址,而不是内容。
关于隐式声明的错误是因为你把command1
和command2
的定义放在了main()
之后。 C 要求在使用函数之前定义或声明函数,因此您要么必须向下移动 main()
,要么将函数原型放在它之前。
您还需要初始化 commandA
和 commandB
。
#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();
}
}