Reputation: 13
char * read_command()
{
char command[25];
char *input = malloc(sizeof(char) * 25);
printf("myRolodex Command: ");
scanf("%c", &command);
strcpy(input, command);
return input;
}
void evaluate_command(char command[250])
{
if (strcmp(command == 'I')==0)
{
printf("\nenterned i");
}
}
int main()
{
evaluate_command(read_command))
return 0;
}
I've tried doing this but i get when compiling:
rolodex.c: In function ‘read_command’:
rolodex.c:25: warning: format ‘%c’ expects type ‘char *’, but argument 2 has type ‘char (*)[25]’
rolodex.c: In function ‘evaluate_command’:
rolodex.c:32: warning: comparison between pointer and integer
rolodex.c:32: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast
/usr/include/string.h:143: note: expected ‘const char *’ but argument is of type ‘int’
rolodex.c:32: error: too few arguments to function ‘strcmp’
rolodex.c: In function ‘main’:
rolodex.c:43: warning: passing argument 1 of ‘evaluate_command’ from incompatible pointer type
rolodex.c:30: note: expected ‘char *’ but argument is of type ‘char * (*)()’
rolodex.c:43: error: expected ‘;’ before ‘)’ token
rolodex.c:43: error: expected statement before ‘)’ token
im supposed to be making "read_command can print a prompt, then read a character from the user. It may want to ensure the command is a valid command before returning it.
evaluate_command can take a command character and decide which command it is, then do the appropriate action."
Upvotes: 0
Views: 559
Reputation: 41749
An explanation of the compiler errors. There are other problems with your code, though
char * read_command()
{
char command[25];
char *input = malloc(sizeof(char) * 25);
printf("myRolodex Command: ");
scanf("%c", &command);
rolodex.c:25: warning: format ‘%c’ expects type ‘char ’, but argument 2 has type ‘char ()[25]’
So this is saying the second argument is the wrong type. You want to put the character in the first element of command
. To do that, you need to pass a a pointer to that first character. You can do this by passing hte address of that character -&command[0]
, altthough that's just the same thing as command
.
strcpy(input, command);
return input;
}
void evaluate_command(char command[250])
{
if (strcmp(command == 'I')==0)
rolodex.c:32: warning: comparison between pointer and integer
There are two comparisons in this line. The ==0
is comparing with the return value of strcmp
, and that returns an integer. So it can't be that one.
For the other one, we're comparing a pointer-to-char (command
) with a character literal (which is just an integer, to C). So that's wrong.
rolodex.c:32: warning: passing argument 1 of ‘strcmp’ makes pointer from integer
The result of a comparison is (in C) an integer. But you need to pass a string to strcmp
.
rolodex.c:32: error: too few arguments to function ‘strcmp’
Also strcmp
takes two parameters, so what's going on here? You mean strcmp(command, "I")
presumably.
{
printf("\nenterned i");
}
}
int main()
{
evaluate_command(read_command))
rolodex.c:43: warning: passing argument 1 of ‘evaluate_command’ from incompatible pointer type rolodex.c:30: note: expected ‘char *’ but argument is of type ‘char * (*)()’ rolodex.c:43: error: expected ‘;’ before ‘)’ token rolodex.c:43: error: expected statement before ‘)’ token
readcommand is a function. You have to call it (with readcommand()
)
return 0;
}
Upvotes: 1
Reputation: 399713
In your call to evaluate_command()
, you're passing read_command
, which evaluates to a pointer to the function read_command
.
That's not right, you want to call the function and pass its return value to evaluate_command
.
Thus, make it:
evaluate_command(read_command());
^
|
make
call
That said, there are other problems in your code, for instance inside read_command
you're trying to strcpy()
from an uninitialized string, that'll give you undefined behavior.
You can rewrite read_command()
as:
char * read_command(void)
{
char cmd;
if(scanf("%c", &cmd) == 1)
{
char *input = malloc(25);
if(input != NULL)
{
input[0] = cmd;
input[1] = '\0'; /* Make sure it's a valid terminated string. */
}
return input;
}
return NULL;
}
Note that it will return NULL
on failure.
Upvotes: 0