0

I'm asking the user which environment variable he want to know and then I scan it with scanf. But it doesnt work for me. Here is my code:

    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>


    int main(int argc, char *argv[]){

        char *value;
        char input;

        printf("Which variable do you want?\n");
        scanf("%s", input);

        value = getenv(input);

        printf("%s", value);

    }

The compiler says:

"Function argument assignment between types "const char*" and "char" ist not allowed"

So i tried to change the input variable to: char const *input

Now there is no compiler error, but when I submit a name, for example "USER", I get a "Segmentation fault (core dumped)" error.

fluter
  • 13,238
  • 8
  • 62
  • 100
Nono
  • 1,073
  • 4
  • 23
  • 46
  • 4
    `char input; ... scanf("%s", input);` --> `input` is not big enough to store user text input. (Similar problem with http://stackoverflow.com/q/37119782/2410359) – chux - Reinstate Monica May 09 '16 at 15:44
  • when calling any of the `scanf()` family of functions, 1) always check the returned value (not the parameter value) to assure the operation was successful. 2) when using the '%s' input conversion specifier, always use a 'max length' modifier (that is one less than the length of the input buffer) so the user cannot overrun the input buffer (which would result in undefined behaviour and can lead to a seg fault event). BTW: how to you expect to read in a string (NUL terminated char array) when the input buffer is only 1 char long? – user3629249 May 10 '16 at 02:11
  • regarding this line: `printf("%s", value);` the output will sit in the stdout buffer until the program exits, then (and only then) will it be actually displayed on the terminal. Strongly suggest using: `printf("%s\n", value);` because the '\n' (newline) will cause the stdout buffer to be displayed on the terminal immediately. – user3629249 May 10 '16 at 02:14

3 Answers3

4

The warning is because here

value = getenv(input);

you pass a char to getenv(), which has the prototype:

   char *getenv(const char *name);

Define input as a char array like:

    char input[256];

    printf("Which variable do you want?\n");
    scanf("%255s", input); //specify the width to avoid buffer overflow

Or, you can use dynamically memory allocation (using malloc) if you think 256 is not big enough for your purposes.

P.P
  • 117,907
  • 20
  • 175
  • 238
2

When you defined char *input; you satisfy the compiler because your syntax is valid: when calling scanf("%s", input); you are saying you want a string and it should get placed wherever input is.

The problem is input isn't anywhere (initialized) yet... where it points is undefined at the moment; before using any pointer you must make it point somewhere that is valid (and large enough to hold whatever you intend to put there).

There are a few ways you can solve this, but perhaps the easiest is to decide how large the input needs to be and declare a character array, such as: char input[512];. Be aware that this is problematic because if the input exceeds your buffer you will overwrite other memory... but this should get you moving forward for now.

mah
  • 39,056
  • 9
  • 76
  • 93
1

char is a single char.
char *input declares a variable which hold a pointer to a character, but there is no memory for the data. In C, this is a correct behavior. However, sscanf expects that you actually pass a pointer which points to allocated memory (please consider that the function does not return any pointer, so it has no chance of allocating memory for you).

So between declaration and use, please use malloc to allocate memory.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Alexander Kemp
  • 202
  • 1
  • 10