Core Dump in C. Memory Access

Asked

Viewed 9 times

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 as int and should therefore use: scanf("%d",&sDados[i].iIdade);.

  • True! I hadn’t noticed that the old scanf was to receive string (%s)! Thank you!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.