0

My function is returning the following error when executing the program: Process returned -1073741819 (0xC0000005).

Until the moment I assign the inputs to the variables, it works. However, when the program is going to assign the values of the variables to the pointers, the program stops.

void Cadastrar(pessoas *p){
    char nome[50];
    int idade;
    char sexo;
    char endereco [200];

    printf("Digite o nome: ");
    fflush(stdin);
    scanf("%s",nome);
    fflush(stdin);

    printf("Digite a idade: ");
    scanf("%d",&idade);

    printf("Escolha o sexo: ");
    fflush(stdin);
    sexo = getchar();
    fflush(stdin);

    printf("Digite o endereco: ");
    fflush(stdin);
    scanf("%s",endereco);
    fflush(stdin);

    strncpy(p->nome,nome, sizeof(nome));
    p->idade = idade;
    p->sexo = sexo;
    strncpy(p->endereco,endereco, sizeof(nome));

}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • By now the older question is better. Please work on that. – Yunnosch May 18 '21 at 11:01
  • 1
    `fflush(stdin);` is undefined behaviour. Don't do it and don't learn from those who do. https://stackoverflow.com/questions/2979209/using-fflushstdin Also, `strncpy(p->endereco,endereco, sizeof(nome));` one of these things is not like the other two. Also `strncpy` is a dangerous function that should almost never be used on its own. – n. m. could be an AI May 18 '21 at 11:08
  • 1
    If you get the inputs read (so `endereco` is given a value), then the problem is almost certainly in the structure that `p` points to. You've not provided any evidence to show how the structure is defined as a type, nor how you've initialized `p`. There's a moderate chance that `p` is a null pointer; there's a chance that it simply points somewhere random because it was not initialized. But we can't help with that because you've not provided the information to allow us to do so. – Jonathan Leffler May 18 '21 at 11:09
  • You need to show the code that calls `Cadastrar` and the relevant variable declarations. The problem is most likely _there_ – Jabberwocky May 18 '21 at 11:52

0 Answers0