Possible Duplicate:
Segmentation Fault – C
#include<stdio.h>
#include<string.h>
int main()
{
char *p;
printf("enter some thing:");
gets(p);
printf("you have typed:%s\n",p);
}
Why doesn’t this program work?
i can’t use pointer as a string.
Output is:
enter some thing:raihan
Segmentation fault (core dumped)
I get this error every time when I use a char pointer.
How can I solve this problem?
I am using code-blocks on Linux mint13 KDE.
You have not allocated memory. You just declared a pointer,
p, but didn’t make it point at anything. That explains the segmentation fault. You will need to allocate memory for your buffer.What’s more,
getsdoes not allow you to specify how big the buffer is. So you are at risk of running over the end of the buffer. So usefgetsinstead.I also corrected your declaration of
mainand made sure that it returns a value.