I have a very simple program that checks the user argument and prints something. Here it is:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const char * foo(char * input){
char *result = NULL;
strcpy(result, "{ ");
if (strcmp(input, "kittycat") == 0){
strcat(result, "JACKPOT!");
}
else{
strcat(result, "Nothing");
}
strcat(result, " }");
return result;
}
int main(int argc, char *argv[]){
printf("%s\n", foo(argv[1]));
printf("%s\n", foo(argv[1]));
printf("%s\n", foo(argv[1]));
return 0;
}
In main(), if I print printf("%s\n", foo(argv[1])); just once, the program runs without errors. However, if I print it three times as shown above, I get "Segmentation fault: 11". Any ideas? I know I can simplify foo and avoid the use of "char *result", but I would like to understand what's wrong with my usage of "char *result".