Reputation: 1001
I am getting a strange error that's saying that my variables are not declared even though I have declared them in main. Am I missing something?
Error 4 error C2065: 'destination' : undeclared identifier c:\users\owner\documents\visual studio 2012\projects\project36\project36\source.c 26 1 Project36
I am programming in C.
variable declaration:
char sourcePiece;
char destination;
function call:
askForMove(sourcePiece, destination);
function def:
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);
}
prototype:
void askForMove(char, char );
Upvotes: 0
Views: 1245
Reputation: 320371
The full version of the code and the latest screenshot with the errors that you posted indicate that the compiler complains about the local variable declarations made in main
function. The compiler complains because the variable declarations are interleaved with statements inside main
, which was not supported by "classic" C language (C89/90). In order to compile this code you need a C99 (or later) compiler.
The code is easy to fix for a pre-C99 compiler - just move all local variable declarations to the beginning of the enclosing block (i.e. to the beginning of main
in your case).
Upvotes: 1
Reputation: 4536
You Should Know that
Formal parameters, are treated as local variables within a function.
So Here You are duplicating them and it is causing error.
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);
}
Remove Them
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);
}
Also Note your question is not a good example of how one should write , Always Post Minimal, Complete, and Verifiable example.
Update What AnT is saying make sense , see this C89, Mixing Variable Declarations and Code
Upvotes: 1
Reputation: 144
I'm not sure what you intended to achieve in your program but you have a duplication in your variable names. Don't use the same name for the function argument and the local variable.
Upvotes: 0
Reputation: 57033
As it has been noted by some commenters, one of the problems is that you cannot have a local variable and a formal parameter with the same name. I suggest that you remove the declarations of the local variables, because it's the parameters that you want to use in your function, not them.
Upvotes: 2