0

I have a code and can't figure out why it's crashing (segmentation fault) I know from past posts that it has something to do with unaccessable memory (I think), but I initialized my "input" variable.

#include <stdio.h>
#include <stdlib.h>
char *getInfo() {
   char input[1000];
   scanf("%s", input);
   return input;
}
int main() {
   char *x;
   x = getInfo();
   printf("%s\n", x);
   return 0;
}

When I run and backtrace the program inside gdb, it says (among other things) "... in main () at error.c:11" When I break at line 11 after giving input ("bark") try to print the variables, print input gives me '\000 x29' and print x gives me 0x0. I know that 0x0 means that it's null, and I think \000 also means null, but I don't get why., when I scaf'ed input, shouldn't the null be replaced?

Cencoroll
  • 37
  • 1
  • 5
  • 6
    Does this answer your question? [Returning an array using C](https://stackoverflow.com/questions/11656532/returning-an-array-using-c) – Christopher Schneider May 01 '20 at 16:21
  • 1
    You're creating the `input` array as a local variable in the function, and trying to return a pointer to it. However the local variable goes out of scope after the function executes, which results in a pointer to garbage. A quick fix would be to make the variable `static`, but this would give the array static lifetime and you probably don't want that. Alternatively you could create the array in main and pass it to the function to be updated. Another option would be to dynamically allocate the array using `malloc` and then `free` it later when you're done, such as at the end of `main`. – MrHappyAsthma May 01 '20 at 16:23

1 Answers1

1

Your function getInfo returns the address of the local variable input. But this variable is deleted when the function returns, so the pointer is becoming invalid. The regular way to solve this is by passing the array into the function as a parameter.

char *getInfo(char *input) {
   scanf("%s", input);
   return input;
}

and on the caller side:

char input[1000];
char *x = getInfo(input);

Of course in your case I would just move the call to scanf into main:

char input[1000];
scanf("%s", input);
printf("%s\n", input);

Please also note that your call to scanf is not safe as scanf might read more input than the buffer can hold. Also my very simple change for getInfo has the disadvantage that you do not pass the length. Normally you should pass the pointer to the buffer and the size of the buffer. That way you can make sure that the function does not overflow the buffer.

For safer variants in your case you might also consider one of these lines:

scanf("%999s", input);
fgets(input, 1000, stdin);
Werner Henze
  • 16,404
  • 12
  • 44
  • 69
  • 1
    "I would just do" `scanf("%s", input);` --> Suggest `scanf("%999s", input);` or `fgets()` to demo to OP the idea of "make sure that the function does not overflow the buffer.". Else you might as well code [`gets()`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). – chux - Reinstate Monica May 01 '20 at 16:44