编译器错误变量声明

编译器错误变量声明

问题描述:

我得到一个奇怪的错误,说我的变量没有被声明,即使我已经在main中声明了它们。我错过了什么吗?编译器错误变量声明

错误4错误C2065:目的地:未声明的标识符C:\用户\所有者\文件\视觉工作室2012 \项目\ 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); 
+1

你能显示整个代码吗? –

+3

是不是你复制char sourcepiece和函数中的目的地。 –

+0

我认为参数名称并不重要。他们是不同的,不是吗?是的,我可以发布整个代码。 –

全面和版本的代码与您发布错误的最新截图表明,编译器抱怨中所作的局部变量声明main功能。编译器会抱怨,因为变量声明与main内部的语句交错,而“经典”C语言(C89/90)不支持该语句。为了编译此代码,您需要一个C99(或更高版本)编译器。

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

+0

啊,有效的,谢谢!!! –

+0

@ Shinji-san总是发布可验证的代码和有错误的代码,对于这个人来说很容易努力解决你的问题:) –

+0

先生,我需要和你谈谈,我们可以聊天吗? –

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

我不确定你想在你的程序中实现什么,但是你的变量名有重复。不要为函数参数和局部变量使用相同的名称。

你应该知道,

形式参数,作为一个函数内的局部变量处理。

所以在这里您正在复制它们并导致错误。

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); 

} 

也注意到你的问题不是一天应该怎么写一个很好的例子,始终邮政Minimal, Complete, and Verifiable example

更新 什么蚂蚁说是有意义的,看到这个C89, Mixing Variable Declarations and Code

+0

谢谢。但它现在阻止了我为这些参数分配任何东西。语句'x = 4'正在生成errors.void askForMove(char x,char y){x = 4}正在产生错误。 –

+0

@ Shinji-san CAn请给我看整个代码? –

+0

4是整数,char不是,如果你想存储符号4,使用x ='4'; –