Hi,
I have problems with the strings size.
My program is the following:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
FILE * fp;
char input[20];
char *str;
printf("Enter the file name: ");
scanf("%s", input);
printf("The file name is %s\n", input);
printf("Sizeof %s is %lu\n", input, sizeof(input));
fp = fopen("file.txt", "w");
fprintf(fp, "%s", input);
fclose(fp);
sprintf(str, "%s", input);
puts(str);
return 0;
}
Why fprintf works well whereas the sprintf causes the segmentation fault?
I noticed that changing the size of the input array from 20 to (at least) 41 solves the problem and sprintf works as well. Why?