• 0

[C] Segmentation fault: core dumped


Question

Could someone explain to me what a segmentation fault is, and what is causing it within my program? I'd be grateful for any help! :D

#include <stdio.h>
#define ARR 50

void Fill_Arrays(int ID[], int max, double score[], int *count);
double average(const int score[], int count1);

int main (void)
{
FILE *outp;
outp = fopen("output.out", "w");
int ID[ARR], count = 0, input_status;
double score[ARR], av;
Fill_Arrays(ID, ARR, score, &count);
av= average(score[], count);
fclose(outp);
return (0);
}


void Fill_Arrays (int ID[], int max, double score[], int *count)
{
FILE *inp;
int input_status, data1;
double data2;
inp = fopen("assignment8.dat", "r");
input_status= fscanf(inp, "%d%lf", data1, data2);
while (input_status != EOF && *count < max){
ID[*count] = data1;
score[*count] = data2;
*count++;
input_status= fscanf(inp, "%d&lf", data1, data2);
}
fclose(inp);
}

double average(const int score[], int count1)
{
int counter;
double sum=0, average;
for (counter = 0; counter <= count1; counter++)
sum+=score[counter];
average=sum/count1;
return (average);
}

Edited by togamonkey
Link to comment
https://www.neowin.net/forum/topic/327665-c-segmentation-fault-core-dumped/
Share on other sites

3 answers to this question

Recommended Posts

  • 0

It's in your Fill_Arrays function. fscanf expects pointers, you're giving it the values inside your variables instead, thus causing a seg fault. You should also check before you use FILE* inp that inp is not NULL. You've also got some errors in switching between data types. Look over this program again and I bet you'll spot some of your errors.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.