• 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.
  • Posts

    • Google's financial performance remains unaffected by competition from ChatGPT by Pradeep Viswanathan Today, Alphabet (Google's parent company) announced earnings for the quarter ended June 30, 2025. Overall revenue was $96.4 billion, up 14% YoY, thanks to the strong performance of Google Search & other, YouTube ads, Google subscriptions, platforms, and devices, and Google Cloud. Each of these businesses delivered double-digit growth in Q2. Google Cloud posted the strongest growth at 32% YoY, driven by the growing momentum of Google Cloud Platform (GCP), which includes AI Infrastructure and Generative AI Solutions. Its annual revenue run-rate is now more than $50 billion. In addition to revenue growth, Alphabet also posted strong margin improvement, reporting an operating income of $31.2 billion, up 32% YoY. Sundar Pichai, Alphabet CEO, said: During the earnings call, Sundar Pichai also revealed that the Gemini App now has more than 450 million monthly active users and that daily requests grew by over 50% since the last quarter. Despite ChatGPT’s explosive growth, Google appears unaffected so far, thanks to the strong financial performance of Google Search and the growing momentum of Gemini. However, it remains to be seen whether Google can hold its ground as ChatGPT increasingly attracts younger users who are turning to it over traditional Google searches.
    • Here is a better view 
    • I care more about ethical LLMs than open source LLMs. Would be great if they declare that the training data was sourced without copyright infringement.
    • Yep social media addiction is no different than alcohol or cigarettes.
    • I strongly feel it is a test bed for foldables. No other reason to slim down the phone even further, despite have a massive camera bump.
  • Recent Achievements

    • Collaborator
      fernan99 earned a badge
      Collaborator
    • Collaborator
      MikeK13 earned a badge
      Collaborator
    • One Month Later
      Alexander 001 earned a badge
      One Month Later
    • One Month Later
      Antonio Barboza earned a badge
      One Month Later
    • Week One Done
      Antonio Barboza earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      590
    2. 2
      ATLien_0
      225
    3. 3
      Michael Scrip
      167
    4. 4
      Xenon
      139
    5. 5
      +FloatingFatMan
      128
  • Tell a friend

    Love Neowin? Tell a friend!