-1

I'm trying to return a struct from a function but trying to print its content in main but all I get is gibberish this is the struct

struct date
{
    int jour;
    int mois;
    int annee;
};
typedef struct date DATE;

struct client
{
    char nom[50];
    char prenom[50];
    char cin[12];
    DATE date_naiss;
    char num_passport[10];
    int a;
};

This is my Main function

void main()
{
    CLIENT clt;
    clt=creer_client();
    afficher_client(clt);
}

This is the function that returns the struct

CLIENT creer_client()
{
    CLIENT clt;
    printf("Donner le nom du client : ");
    fgets(clt.nom, 50, stdin);
    printf("Donner le prenom du client : ");
    fgets(clt.prenom, 50, stdin);
    printf("Donner le CIN du client : ");
    fgets(clt.cin, 12, stdin);
    while (cntrl_cin(clt.cin) == false)
    {
        fgets(clt.cin, 12, stdin);
    }
    printf("donner la date de naissance");
    scanf("%d%d%d", &clt.date_naiss.jour, &clt.date_naiss.mois, &clt.date_naiss.annee);
    getchar();
    printf("donner le numero de passeport : ");
    fgets(clt.num_passport, 10, stdin);
}

And this is the function that prints the struct

void afficher_client(CLIENT clt)
{
    printf("nom: %s \tprenom:%s\ncin:%s\ndate de naissance: %d/%d/%d \nnumero passeport: %s", clt.nom, clt.prenom, clt.cin, clt.date_naiss.jour, clt.date_naiss.mois, clt.date_naiss.annee, clt.num_passport);
}
```
Dhia Ammar
  • 49
  • 4
  • 1
    There is no `return` statement in that function, and I'm sure the compiler told you that. Enable warnings, read the warnings, fix the warnings. – user3386109 Dec 01 '21 at 19:48

1 Answers1

3

You seem to be missing a return statement:

CLIENT creer_client()
{
    CLIENT clt;
    /* all the stuff you had before */
    return clt; /* <- You were missing this */
}

I am surprised your compiler did not give you a warning or error about this.

idz
  • 12,825
  • 1
  • 29
  • 40
  • If the compiler didn't give a warning, then either the OP needs to add options to get relevant warnings, or they need to upgrade to a better compiler that does give the warnings. – Jonathan Leffler Dec 01 '21 at 20:13
  • I'm using VScode with the gcc compiler and when I hit alt+ctrl+N to compile it doesn't show any warnings any solutions for that? – Dhia Ammar Dec 01 '21 at 20:36
  • @DhiaAmmar [This question may help](https://stackoverflow.com/questions/44947692). The build output pane should be automatically shown when you start a build, and you need to make sure that it doesn't get hidden when the build finishes. – user3386109 Dec 01 '21 at 22:48