Goodevening, I'm starting coding in C and I've done a little program to understand "struct" function, but... I receive this message: Segmentation fault: 11
I've understood a bit what this problem is about, but I can't understand where it borns on my program.
Can you help me? I tank you!

#include <stdio.h>
#include <stdlib.h>

typedef struct Joueurs Joueurs;

struct Joueurs
{
    char nom[100];
    char prenom[100];
    char adresse[1000];

    int age;
    int garcon;
};

int main(int argc, char *argv[])
{
    Joueurs joueur[1];

    int i = 0, nombreDeJoueurs = 2;


    for (i = 0; i <  nombreDeJoueurs; i++) {

        printf("Quel est votre nom ? ");
        scanf("%s", joueur[i].nom);
        printf("Votre prenom ? ");
        scanf("%s", joueur[i].prenom);
        printf("Quelle est votre adresse ? ");
        scanf("%s", joueur[i].adresse);
        printf("Quelle est votre age ? ");
        scanf("%d", &joueur[i].age);
        printf("Est tu un garcon ? (repondre 1 = oui, 0 = non) ");
        scanf("%d", &joueur[i].garcon);

        printf("\n");

        printf("Vous vous appelez %s %s, vous habitez %s, vous avez %d ans et vous etes une fille \n", joueur[i].prenom, joueur[i].nom, joueur[i].adresse, joueur[i].age);

        //if (joueur[i].garcon == 0) {
       //     printf("Vous vous appelez %s %s, vous habitez %s, vous avez %d ans et vous etes une fille \n", joueur[i].prenom, joueur[i].nom, joueur[i].adresse, joueur[i].age);
        //}
        //else{
          //  printf("Vous vous appelez %s %s, vous habitez %s, vous avez %d ans et vous etes un garcon \n", joueur[i].prenom, joueur[i].nom, joueur[i].adresse, joueur[i].age);}
    }
}

Inline Code Example Here

Recommended Answers

All 2 Replies

Your joueur array holds only one element, but your index goes up to 1 (which would be the second element), which is out of bounds.

LOL
thanks btw

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.