So I'm starting to learn C, tried to make a little program to learn about structures. It asks two "players" to enter their name and age, and then displays it back.
When I run it, I get a segmentation fault after entering the second player's last name. I don't get why.
I tried to run it in gdb and got the following message :
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7e5288c in _IO_vfscanf () from /lib/x86_64-linux-gnu/libc.so.6
Here is my code, struct.c :
#include "struct.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
Person player[1];
int i = 0;
for (i = 0; i < 2; i++) {
printf("\n=== Player %d ===\n", i+1);
printf("First name : ");
scanf("%s", player[i].firstName);
printf("Last name : ");
scanf("%s", player[i].lastName);
printf("Age : ");
scanf("%d", &player[i].age);
}
for (i = 0 ; i < 2; i++) {
printf("\n=== Player %d ===\n", i+1);
printf("%s %s, %d years old", player[i].firstName, player[i].lastName, player[i].age);
}
return 0;
}
And here is the header, struct.h :
#ifndef DEF_STRUCT
#define DEF_STRUCT
#define CHAR_SIZE 100
typedef struct Person Person;
struct Person {
char firstName[CHAR_SIZE];
char lastName[CHAR_SIZE];
int age;
};
#endif
Thanks!