Reputation: 33
I don't understand what my error is:
void main(char string[])
{
strcpy(command_E,string);
processCMD(); /*FIRST ERROR: prototype function declaration not in scope */
}
void processCMD(void) /*SECOND ERROR: external item attribute mismatch */
{
.... /*rest of code not displayed*/
Upvotes: 1
Views: 6307
Reputation: 340198
You need a prototype before the the first use of the function to declare it so the compiler knows what kind of arguments the function takes (if any) and what it returns:
void processCMD(void); // prototype
void main(char string[])
{
strcpy(command_E,string);
processCMD();
}
void processCMD(void)
{
.... /*rest of code not displayes*/
Alternatively, you could defined the function before it's first use (since the definition will provide the information the compiler wants.
Typically, the prototype will go in a header file so other modules can use the function since function definitions can only be in one source file (barring such things as making the function static, which makes each instance of the function 'private' to a module, or marking the function as inline, which enables multiple definitions).
Some additional prototype trivia:
Generally in C, the lack of a protoype is not an error in itself - if the compiler sees a function call without having seen a prototype it will assume that you're passing the right parameters, it will apply 'default promotions' to paramters (basically convert things to int
ot double
as appropriate), and will assume the function returns an int
. If those assumptions are wrong, then the program is incorrect and will have undefined behavior. Compilers can often be configured to emit warnings or errors for calling functions that don't have prototypes (apparently yours has). In C++, calling a function without a previous protoype being seen (or the full function definition) is always an error.
In C, there's a difference between:
void processCMD();
and
void processCMD(void);
The first is a function declaration, but not a prototype - it tells the compiler that processCMD
is a function and that it returns void
instead of int
. But it doesn't tell the compiler anything about the arguments. The compiler will still allow function calls to be made with arguments passed and will apply the default promotions. (Again, this feature might be disabled by compiler configuration).
The second is a prototype that specifically tells the compiler that the function takes no arguments.
In C++, both are equivalent and specifically tell the compiler that the function takes no arguments.
Upvotes: 1
Reputation: 881383
At the point where you use processCMD()
, you haven't declared a prototype for it, so it gets a default one.
The fact that you haven't declared it causes the first error.
The fact that your actual definition conflicts with the default one created because you hadn't declared it is the cause of your second error.
The solution is to either define the function before use:
void processCMD(void) {
blah blah blah
}
void main (char string[]) { // not really a good definition for main, by the way.
strcpy(command_E,string);
processCMD();
}
or provide a prototype before use:
void processCMD(void);
void main (char string[]) { // not really a good definition for main, by the way.
strcpy(command_E,string);
processCMD();
}
void processCMD(void) {
blah blah blah
}
As to the declaration of main
, the two canonical forms are:
int main (void);
int main (int argc, char *argv[]); // or char **argv.
Others are allowed by the standard (implementation defined) but those two are required (at least for hosted implementations - freestanding implementations like embedded systems or operating systems can pretty well do whatever they want).
Upvotes: 4