0

I dont understand why my program crashs when tryign to fill in some DATA to my struct table declared in main ... I know that a table is itself a pointer right ? So basically i dont realy need to pass the address using &var when using scanf ... but thats when it crashes for me ..

when i tried to pass inputs by scanf("expression",&T[i].var) .. that worked but when showing my content i found out :

Problem Image

As you can see I couldnt event put something for my stat and values totally diffrent from the inputs .. probably cuz taking memory adresses .. but why the Id actually worked and also Lieu is null ? so weird .

struct ChDep
{
int id;
int mins; 
char nom[10];
char lieu[10];
double prix;
char stat;
};


//Function Fill some content to my Table
void RemplireHCD(int n, struct ChDep *T)
{

for (int i = 0; i < n; i++)
{
    printf("Mission Numero : %d\n", i);
    printf("ID = ");
    scanf("%d", &T[i].id);
    printf("\nDuree = ");
    scanf("%d", &T[i].mins);
    printf("\nNom = ");
    scanf("%s", &T[i].nom);
    printf("\nLieu = ");
    scanf("%s", &T[i].lieu);
    printf("\nPrix = ");
    scanf("%lf", &T[i].prix);
    printf("\nStatut = ");
    scanf("%c", &T[i].stat);
    system("cls");
}
}

//Function to show my Struct Table Content
void AfficherHCD(int n, struct ChDep *T) 
{
system("cls"); 
for (int i = 0; i < n; i++)
{
    printf("%d > [ ID: %d - Duree : %d - Libelee : %s - Lieu : %s - Prix    : %lf - Statut : %c ]\n",
        T[i].id, T[i].mins, T[i].nom, T[i].lieu, T[i].prix, T[i].stat);
}
}


int main(int argc, char *argv[])
{
struct ChDep HCD[20];
int n;

printf("SVP donnez Le Nombre de Mission A remplire : \n");
scanf("%d", &n);
system("cls");

RemplireHCD(n, HCD);
AfficherHCD(n, HCD);

getchar();
return 0;
}
Saad Amrani
  • 95
  • 2
  • 11
  • 2
    In your `printf` in the `AfficherHCD()` function you forgot to provide one argument (the first one). Voting to close as typo. – 001 Jun 14 '19 at 11:39
  • 1
    `scanf("%c", &T[i].stat);` --> `scanf(" %c", &T[i].stat);` notice space before `%`. – kiran Biradar Jun 14 '19 at 11:43
  • 1
    @JohnnyMopp That was not whole the problem as OP described in the problem statement. – kiran Biradar Jun 14 '19 at 11:44
  • @kiranBiradar But it is the one causing the crash which is what op is asking about. – 001 Jun 14 '19 at 11:44
  • @JohnnyMopp Yep true, but not complete. I mean to close the question as typo – kiran Biradar Jun 14 '19 at 11:45
  • @JohnnyMopp thanks , but still read the whole problem pls dont rush for closing my topic prob please . – Saad Amrani Jun 14 '19 at 11:56
  • "*I know that a table is itself a pointer right*" -- wrong. C does not have a built-in concept of "table" but I see no reasonable way to interpret that term in C context that would make the statement true. In particular, I suspect that by "table" you mean an array of `struct ChDep` objects, and arrays and pointers have a close relationship, they are very different things. – John Bollinger Jun 14 '19 at 11:57
  • For the `%s` format specifier, you should not use the `&` in `scanf("%s", &T[i].nom);` but simply `scanf("%s", T[i].nom);` (and others). – Weather Vane Jun 14 '19 at 12:35
  • @WeatherVane Please Read what i said ... – Saad Amrani Jun 14 '19 at 19:08
  • @JohnBollinger well yeah i meant array it is diffrent indeed from a pointer but kinda can be said a pointer , any news about the problem tho ? – Saad Amrani Jun 14 '19 at 19:09
  • The format specifier `%d` needs to be passed the address of the target variable **explicitly**. With the format specifier `%s` the array decays to a pointer. Often, there won't be a problem but if the target variable is a function argument - already a pointer, or if its a `struct` member that is a pointer - it's most definitely going to cause damage. – Weather Vane Jun 14 '19 at 19:13
  • everything fixed now But the character (char stat) .. when i get to the point where i must enter and input for it it skips that without me being able to actualy type something ... ? and it shows as empty char ? why is that ? – Saad Amrani Jun 14 '19 at 19:14
  • 2
    Change `scanf("%c", &T[i].stat);` to `scanf(" %c", &T[i].stat);`. Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). Most format specifiers filter leading whitespace, but `%c` does not. The added space instructs it to. Please [read](https://stackoverflow.com/questions/56597237/how-do-i-fill-in-correctly-my-struct-table-without-my-program-crashing?noredirect=1#comment99770952_56597237) what @kiranBiradar said. – Weather Vane Jun 14 '19 at 19:16

0 Answers0