What could be the possible reasons for segmentation fault?

Segmentation fault Core Dumped. C program?

  • I have to write a program for my class and I am having a bit of trouble with the file pointer. #include <stdio.h> #include <string.h> int main(void) { char stone_color[82]; char column[82]; int row[82]; int i; int x; FILE *fp; fp = fopen("goboard1.txt", "r"); if ( fp == NULL ) { printf(" Not a valid pointer! \n"); } i == 0; while( fscanf(fp, "%s", stone_color[i]) != EOF) { fscanf(fp, "%s", column[i]); fscanf(fp, "%d", row[i]); i++; } return (0); } This is what I have right now. It points to a file that has 3 columns of numbers/letters like this b A 1 b B 4 B b 1 B C 2 B C 3 W A 3 w A 1 W B 3 W B 2 What I'm trying to do is scan those into arrays and later print them. The first column is stone_color the second is column and the third should be row. Everything compiles correctly however when I run it I get a segmentation fault. Any ideas?

  • Answer:

    A segmentation fault is usually the result of trying to dereference a null pointer. Now, you do check to see if fp is null, and you print an appropriate message. But what happens after that? You go right on and do all of the file access anyway, causing a segmentation fault if fp is null. You should probably put the rest of the function in an else block after the if statement. (Alternatively, you could add a return statement to the if block.) As to why the pointer is null, well, maybe the filename is wrong, or it's in the wrong place, or you don't have permission to access it. You can check the errno value to see more specific information on what went wrong.

RobDog at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.