2

My code is:

#include <stdio.h>
#include <string.h>
char *getUserInput() {
    char command[65];

    //Ask the user for valid input
    printf("Please enter a command:\n");
    fgets(command, 65, stdin);

    //Remove newline
    command[strcspn(command, "\n")] = 0;
    return command;
}

int main() {
    char *recCommand = getUserInput();
    printf("%s", recCommand);
    return 0;
}

When this code is executed, this is the console:

Please enter a command:
Test <-- this is the command I entered
*weird unknown characters returned to console*

Why are there weird unknown characters being returned to console instead of "Test"?

7
  • Local variables are disabled when exiting the scope of the function. Commented Apr 10, 2015 at 3:15
  • So instead of returning a pointer to "command", I should be returning the char array command itself? Commented Apr 10, 2015 at 3:15
  • try return strdup(command); this make copy string.( secure the area in heap) then free(command); at main. Commented Apr 10, 2015 at 3:20
  • Why do we need to free(command)? Are you referring to here the command located inside the getUserInput scope or inside the main scope? Commented Apr 10, 2015 at 3:21
  • area of heap isn't release, it will still be secured. So the user must be freed. Commented Apr 10, 2015 at 3:24

2 Answers 2

3

It is because you are returning the value of a local variable. Try putting this:

char *getUserInput() {
    static char command[65];

    //Ask the user for valid input
    printf("Please enter a command:\n");
    fgets(command, 65, stdin);

    //Remove newline
    command[strcspn(command, "\n")] = 0;
    return command;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I come from a C# background, is it possible to instead just return the value of command from the getUserInput function?
@AdamNygate: Yes, it's possible. But it require a bit of work to allocate memory and then make sure it gets released in a timely manner. In C, this is probably your best bet.
The trouble with this is that if you call getUserInput() twice, the second value overwrites the first. This can cause problems in multi-threaded programs where multiple threads call the function, and it can also cause problems in single-threaded programs where you call the function twice before using the returned value from the first call.
3

After doing some more research, it seems that the best way is to actually pass in a char array from the main function in which to store the input from getUserInput.

Here is my revised code:

void getUserInput(char *command) {
    //Ask the user for valid input
    printf("Please enter a command:\n");
    fgets(command, 65, stdin);

    //Remove newline/return carriage
    command[strcspn(command, "\n")] = 0;
}

int main {
    char recCommand[65];
    getUserInput(recCommand);
    printf("%s", recCommand);
    return 0;
}

2 Comments

Hardcoding the length of your array into your function is a bad idea. At least make it void getUserInput(char * command, size_t size) or similar.
I've made the length a macro and defined it in a commonmacros.h file, the requested input will always be 65 chars long (64 + \0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.