Where am I going wrong and why?
#include<stdio.h>
#include<string.h>
int main()
{
char *str;
int length, i, j, flag = 0;
printf("\n\nEnter string: ");
fgets(str, 20, stdin);
printf("You entered: %s", str);
return 0;
}
There's a problem with the line fgets(str, 20, stdin);
line. I cannot figure it out. After entering the string, the compiler just stops working and I get an error saying: This program has stopped working. Could you point where am I going wrong and also a workaround for this problem? The standard library defines fgets
as:
char * fgets ( char * str, int num, FILE * stream );
I'm using Sublime Text 2 and GCC on the MinGW shell.
A different question based on the pointer concept:
Are there any differences between char * str
, char* str
and char *str
?