0
By executing the following code, I can enter the name and age of the first three employees and then return the Core Dump error. From what I read in https://www.geeksforgeeks.org/core-dump-segmentation-fault-c-cpp/, That means I’m trying to access a memory address that doesn’t belong to me. But I can’t understand why it doesn’t belong to me and why I can type the first three. Can you help me?
#include <stdio.h>
struct DADO
{
char iNome[40];
int iIdade;
};
int main(){
struct DADO sDados[5];
int i;
for (i = 0; i < 5; i++)
{
printf("Digite o nome do %d o funcionario: \n",i);
scanf("%s",sDados[i].iNome);
printf("Digita a idade do %d o funcionario: \n",i);
scanf("%s",sDados[i].iIdade);
}
for(i=0; i<5 ; i++){
printf("Funcionario: %s , Idade: %d", sDados[i].iNome, sDados[i].iIdade);
}
return 0;
}
You stated
iIdade
asint
and should therefore use:scanf("%d",&sDados[i].iIdade);
.– anonimo
True! I hadn’t noticed that the old scanf was to receive string (%s)! Thank you!
– Neto Araujo