Segmentation fault occurs when your program tries to read/write invalid memory location which is not accessible to program.
For example, if you try to write on NULL address pointed by pointer, then it can cause segmentation fault.
#include <stdio.h>
int main()
{
*(int*)0 = 0;
return 0;
}
There can be other possible reasons you may try to access invalid address.
1. Pointer is not initialized with any proper address and dereferencing pointer later.
int *ptr;
*ptr = 123;
2. Accessing global array beyond size of array.
#include <stdio.h>
int arr[10]; // global array
int main() {
int i;
for(i=0; i<1000; i++)
arr[i] = 1;
}
This is common cause when you are doing string operations and you may go beyond size of character array.
There could be other causes as well, but in short you would be trying to access invalid memory location that would give you segmentation fault.
If your cause of segmentation is different than 2 possible causes mentioned over there, then share your code here via OnlineGDB "share" button. And lets figure out what causes you segmentation fault.