bs7280
bs7280

Reputation: 1094

Conflicting types with parameters in a function in C

I am trying to do something as simple as pass parameters into a function but I seem to be having a lot of trouble, and I can't seem to figure out how to fix it. FYI this is for a homework assignment that is going to be a basic assembly language simulator.

Here is the code

char cmd_char;
char leadChar;
int location;
short int value;

words_read = sscanf(cmd_buffer, "%d %c%hx x%hx", &cmd_char, &leadChar, &location, &value);

...

done = execute_command(cmd_char, cpu, leadChar, location, value);

...

int execute_command(char cmd_char, CPU *cpu, char leadChar, int location, int value)
{
    ...
}

The string being inputted into sscanf(...) will either be:

m x305f x002c
r r6 x10a5
r r3 x0014
j x3030

There is no problem with scanning the proper values, but when I added the variables leadChar, location, and value as parameters to execute_command, I have been unable to sucessfully call it. See the errors below.

The Error messages

Lab10.c:42:46: error: expected declaration specifiers or '...' be                                   fore '(' token
 int execute_command(char cmd_char, CPU *cpu, (char) void, (int) void, (int) voi                                   d);
                                              ^
Lab10.c:42:59: error: expected declaration specifiers or '...' be                                   fore '(' token
 int execute_command(char cmd_char, CPU *cpu, (char) void, (int) void, (int) voi                                   d);

Lab10.c:277:5: error: conflicting types for 'execute_command'
 int execute_command(char cmd_char, CPU *cpu, char leadChar, int location, int v                                   alue)
     ^
Lab10.c:278:1: note: an argument type that has a default promotio                                   n can't match an empty parameter name list declaration
 {
 ^
Lab10.c:265:20: note: previous implicit declaration of 'execute_c                                   ommand' was here
             done = execute_command(cmd_char, cpu, leadChar, location, value);
                    ^
                                                    ^
Lab10_BenShaughnessy.c:42:71: error: expected declaration specifiers or '...' be                                   fore '(' token
 int execute_command(char cmd_char, CPU *cpu, (char) void, (int) void, (int) voi                                   d);

Upvotes: 0

Views: 114

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 754030

In the line:

words_read = sscanf(cmd_buffer, "%d %c%hx x%hx", &cmd_char, &leadChar, &location, &value);

you tell sscanf() that &location is a short *, but it isn't; it is an int *. You need to change the conversion specification to %x. GCC should tell you about this problem, though you might need to twist its arm gently (-Wall is recommended; IIRC, -Wformat is the specific option that's needed, but -Wall is better if you're not using it already).

In fact, as chux points out in a comment, there's also a type mismatch between %d and &cmd_char, too. Thus, you really need:

words_read = sscanf(cmd_buffer, "%c %c%d x%hx", &cmd_char, &leadChar, &location, &value);

And if you're not sure whether there might be leading blanks on the string, then add a space before the first %c. Note that even though you write a space in the format string, sscanf() treats that as optional white space, so the format will accept both of these:

m x305f x002c
mx305fx002c

and various other inputs too.

Upvotes: 1

Related Questions