Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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
C 编译器错误变量声明_C_Variables - Fatal编程技术网

C 编译器错误变量声明

C 编译器错误变量声明,c,variables,C,Variables,我得到了一个奇怪的错误,即我的变量没有声明,即使我在main中声明了它们。我错过什么了吗 错误4错误C2065:“目标”:未声明的标识符c:\users\owner\documents\visual studio 2012\projects\project36\project36\source.c 26 1 project36 我正在用C语言编程 变量声明: char sourcePiece; char destination; 函数调用: askForMove(sourcePiece, de

我得到了一个奇怪的错误,即我的变量没有声明,即使我在main中声明了它们。我错过什么了吗

错误4错误C2065:“目标”:未声明的标识符c:\users\owner\documents\visual studio 2012\projects\project36\project36\source.c 26 1 project36

我正在用C语言编程

变量声明:

char sourcePiece;
char destination;
函数调用:

askForMove(sourcePiece, destination);
功能定义:

void askForMove(char sourcePiece, char destination) {
    char sourcePiece;
    char destination;
    printf("\nEnter your desired move. First enter the starting position, followed by the ending position in letters: ");
    scanf(" %c %c", &sourcePiece, &destination);

}
原型:

void askForMove(char, char );

正如一些评论者所指出的,其中一个问题是不能有同名的局部变量和形式参数。我建议您删除局部变量的声明,因为它是您希望在函数中使用的参数,而不是它们。

正如一些评论者所指出的,问题之一是,您不能使用具有相同名称的局部变量和形式参数。我建议您删除局部变量的声明,因为它是您希望在函数中使用的参数,而不是它们。

我不确定您打算在程序中实现什么,但您的变量名中有重复项。不要对函数参数和局部变量使用相同的名称。

我不确定您打算在程序中实现什么,但您的变量名称有重复。函数参数和局部变量的名称不要相同。

您应该知道这一点

形式参数在函数中被视为局部变量

所以在这里你复制了它们,这会导致错误

 void askForMove(char sourcePiece, char destination) {
 char sourcePiece; //Redeclaring already present in formal parameter.
 char destination; //Redeclaring already present in formal parameter.
 printf("\nEnter your desired move. First enter the starting position, followed by the ending position in letters: ");
scanf(" %c %c", &sourcePiece, &destination);

}
移除它们

 void askForMove(char sourcePiece, char destination) {
 printf("\nEnter your desired move. First enter the starting position, followed by the ending position in letters: ");
scanf(" %c %c", &sourcePiece, &destination);

}
还要注意的是,你的问题并不是一个很好的例子,说明一个人应该如何写作,总是发帖

更新 蚂蚁说的有道理,看看这个你应该知道

形式参数在函数中被视为局部变量

所以在这里你复制了它们,这会导致错误

 void askForMove(char sourcePiece, char destination) {
 char sourcePiece; //Redeclaring already present in formal parameter.
 char destination; //Redeclaring already present in formal parameter.
 printf("\nEnter your desired move. First enter the starting position, followed by the ending position in letters: ");
scanf(" %c %c", &sourcePiece, &destination);

}
移除它们

 void askForMove(char sourcePiece, char destination) {
 printf("\nEnter your desired move. First enter the starting position, followed by the ending position in letters: ");
scanf(" %c %c", &sourcePiece, &destination);

}
还要注意的是,你的问题并不是一个很好的例子,说明一个人应该如何写作,总是发帖

更新
AnT所说的有意义,请参见此

完整版本的代码和您发布的错误的最新屏幕截图表明编译器抱怨在
main
函数中进行的局部变量声明。编译器会抱怨,因为变量声明与
main
中的语句交错,而“classic”C语言(C89/90)不支持这一点。为了编译此代码,您需要C99(或更高版本)编译器


对于C99之前的编译器,代码很容易修复-只需将所有局部变量声明移到封闭块的开头(即,在您的示例中移到
main
的开头)。编译器会抱怨,因为变量声明与
main
中的语句交错,而“classic”C语言(C89/90)不支持这一点。为了编译此代码,您需要C99(或更高版本)编译器



对于C99之前的编译器来说,代码很容易修复-只需将所有局部变量声明移到封闭块的开头(即,在您的例子中移到
main
的开头)。

您能显示整个代码吗?您不是在函数中复制char sourcepiece和destination吗?我认为参数名称并不重要。他们是不同的,不是吗?是的,我可以发布整个代码。你正在阅读你写的代码吗?您同时在作用域中有两个
char SourcePiece
变量(一个是作为
askForMove
的参数接收的变量,另一个是在
askForMove
中声明的新变量)。问题描述不准确。
askForMove
的代码无效(如上文注释中所述),但引用的错误消息甚至与
askForMove
中的错误不匹配。触发引用的错误消息的第26行的代码在哪里?您能显示整个代码吗?您不是在函数中复制了char sourcepiece和destination吗?不过我认为参数名称并不重要。他们是不同的,不是吗?是的,我可以发布整个代码。你正在阅读你写的代码吗?您同时在作用域中有两个
char SourcePiece
变量(一个是作为
askForMove
的参数接收的变量,另一个是在
askForMove
中声明的新变量)。问题描述不准确。
askForMove
的代码无效(如上文注释中所述),但引用的错误消息甚至与
askForMove
中的错误不匹配。触发引用错误消息的第26行代码在哪里?谢谢。但现在我甚至无法为这些参数赋值。语句“x=4”正在生成错误。void askForMove(char x,char y){x=4}正在生成错误。4是整数,char不是,如果要存储符号4,请使用x='4'@真嗣三:你在编故事。
x=4
除了结尾缺少分号(修改参数不会影响实际参数这一事实)之外没有什么错。@AnT它是char,不应该是char x='4',它不是假引号,我不会这么做。我不知道发生了什么事。当我发布这个问题时,我只是复制了上面显示的错误。我只是想找出编译器出了什么问题。但这绝对不是假的。如果你愿意,我可以再拍一张错误截图。谢谢。但现在我甚至无法为这些参数赋值。语句“x=4”正在生成错误。void askForMove(char x,char y){x=4}正在生成错误。4是整数,char不是