-2

Below is a block of code from my program. I'm getting error of Segmentation fault (core dumped) after entering name and age in first loop.

#include<stdio.h>
#include <string.h>
struct Cricketer
{
    char name[25];
    int age;
    float avg_run;
};

int main(){
struct Cricketer c[3];
int i,j;

for (i=0 ; i<3;i++){
    printf("Enter name: \n");
    scanf("%s",c[i].name);

    printf("Enter age: \n");
    scanf("%d",c[i].age);

    printf("Enter average run: \n");
    scanf("%f",c[i].avg_run);               
}
return 0;
}

And I couldn't find what is causing this program.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
Zachary Dale
  • 739
  • 4
  • 11
  • 30
  • 5
    No it's not `gcc` (the *compiler*) that crashes. It's your program that does when you run it. As for how to solve it, you first use a debugger to locate where in the program it happens. Please read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) by Eric Lippert. – Some programmer dude Jul 27 '17 at 08:03
  • 1
    So is your question "How do you debug a core dump?" If so, you should state your platform and/or your preferred debugger. – David Schwartz Jul 27 '17 at 08:06
  • 1
    Compile with all warnings & debug info: `gcc -Wall -Wextra -g`. Then **use the debugger `gdb`** . You fix-my-code question is off-topic. – Basile Starynkevitch Jul 27 '17 at 08:11
  • 1
    A good compiler will warn you about the problem. If you don't have enough warnings enabled, or don't have a good enough compiler, fix it (add the warning options, or upgrade the compiler). – Jonathan Leffler Jul 27 '17 at 08:12
  • @BasileStarynkevitch - too bad I can only vote once on a comment -- that reason, and that reason alone, enabling *Compiler Warnings* would probably resolve 50+% of the questions posted related to C. – David C. Rankin Jul 27 '17 at 09:51
  • Look carefully at the manual page for `scanf`. – lurker Jul 27 '17 at 10:26

2 Answers2

2

Your error is here:

scanf("%d",c[i].age);

Change it for:

scanf("%d",&(c[i].age));

When using %d you have to pass the address of the int variable. And the same for floats:

scanf("%f",&(c[i].avg_run));

When using scanf, the second argument has to be the address of a variable. With your variable name there is no problem because it already refers to the address of the buffer in which you want to store the string.

Jesferman
  • 1,049
  • 7
  • 12
1

When using scanf, the second argument should be the address of a variable. In your program c[i].age and c[i].avg_run are the variables themselves and not the addresses. Use the & operator to get the address of a variable. For Example, &(c[i].age) or just &c[i].age.

What you are passing to scanf as mentioned above are some numbers which might or might not be valid memory addresses. Thus invoking undefined behavior.

c[i].name happens to be fine because referring to an array (name in this case) by just the name evaluates to the base address of the array.

It is a good idea to have compiler warnings enabled. More important is one reads and understands the warnings. Read your compiler manual for more info.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
babon
  • 3,615
  • 2
  • 20
  • 20