Well, this is basics here. A segfault means you are using memory you do not have access to it.
int main()
{
char* a; // Create a pointer (a pointer can only contains an address (int size)
a[200]; // Trying to access to the byt 200 of your pointer but basicaly do nothing. You are suppose to have a segfault here
fgets(a, 200, stdin); // store your stdin into &a (you may have a segfault here too)
return 0;
};
Depending on many thing, sometimes it may fails, sometimes not. But you are doing something wrong here.
You have to way to fix this. First using a simple array char
#include <stdio.h> /* for stdin */
#include <stdlib.h> /* for malloc(3) */
#include <string.h> /* for strlen(3) */
#include <unistd.h> /* for write(2) */
int main()
{
char str[200];
fgets(str, sizeof str, stdin);
write(1, str, strlen(str)); /* you can receive less than the 200 chars */
return (0);
}
Or if you want to keep using pointers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main()
{
const size_t sz = 200;
char* str;
str = malloc(sz);
fgets(str, sz, stdin);
write(1, str, strlen(str));
}
But anyway, your mistake results from a lack of knowledge about pointer and memory in C.
Good luck with that,