0
    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."

Stephanie
  • 13
  • 1
  • 3

2 Answers2

1

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;
   }
The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134
0

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.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • ah i see, what exactly do you mean by "an uninitialized string"? sorry im very new to c – Stephanie Dec 05 '14 at 08:33
  • It's not so much uninitialised, in fact, but unterminated. You are reading a character into the first element of `command`, but leaving the rest of the `command` array alone. `Strcpy` copies until it hits a zero character, but there may or may not be a zero in the rest of the array. – The Archetypal Paul Dec 05 '14 at 08:35
  • Also `command` is a pointer to the array, so you don't need `&command` in the `scanf` - that's a pointer to a pointer to character, which is not what you want. The error messages are all telling you real problems, please read them carefully and act on what they say – The Archetypal Paul Dec 05 '14 at 08:36
  • @Paul Surely all but one element of the array are uninitialized, making the "string" as a whole uninitialized? If the existance of a terminator can't be guaranteed, it's not a string. – unwind Dec 05 '14 at 08:36
  • every malloc() should have it's free() – Peter Miehle Dec 05 '14 at 08:37
  • Yes, true, which is why I didn't say "not uninitialised" but "not so much uninitialised". The fix is not to initialise the content of the array, but to have a NUL character in it. – The Archetypal Paul Dec 05 '14 at 08:38