Search code examples
cstructdynamic-memory-allocation

Random number when allocation struct integer (int) in C


I'm allocating dynamically the amount of ram that I will need for my program. It is working fine until I came across this problem. This is when I type the content that is going to be inside of my one item that is inside the struct which is referred to as an Integer if I type more than 10 number for example (12345678912) it will send me back, a completely different number.
Here is my code:

#include <stdio.h>
#include <stdlib.h>

struct cadastro
{
char email[50];
char nome[20];
int idade;
int telefone;
};

int main()
{
int quantidade_cadastros;
int decisao = 1;

printf("\nQuantos cadastros deseja realizar?\n");
printf("Quantidade: ");
scanf("%d", &quantidade_cadastros);
getchar();

//Definindo a quantidade de cadastros que podem ser realizados no struct e o nome que sera usado para chamar o struct
struct cadastro *cad;
cad = NULL;

cad = malloc(quantidade_cadastros * sizeof(struct cadastro));

for (int i = 0; i < quantidade_cadastros; i++)
{
    printf("\nDigite o NOME da pessoa que sera inserida no indice %d: ", i);
    fgets(cad[i].nome, 20, stdin);
    printf("Digite o EMAIL da pessoa que sera inserida no indice %d: ", i);
    fgets(cad[i].email, 50, stdin);
    printf("Digite a IDADE da pessoa que sera inserida no indice %d: ", i);
    scanf("%d", &cad[i].idade);
    getchar();
    printf("Digite o TELEFONE da pessoa que sera inserida no indice %d: ", i);
    scanf("%d", &cad[i].telefone);
    getchar();
}

for (int i = 0; i < quantidade_cadastros; i++)
{
    printf("\nNOME: %s", cad[i].nome);
    printf("EMAIL: %s", cad[i].email);
    printf("IDADE: %d", cad[i].idade);
    printf("\nTELEFONE: %d", cad[i].telefone);
    printf("\n");
}

printf("\n");
free(cad);
}

Here is the input I entered:

Quantos cadastros deseja realizar?
Quantidade: 2

Digite o NOME da pessoa que sera inserida no indice 0: Test1
Digite o EMAIL da pessoa que sera inserida no indice 0: Email1
Digite a IDADE da pessoa que sera inserida no indice 0: 19
Digite o TELEFONE da pessoa que sera inserida no indice 0: 1234567891

Digite o NOME da pessoa que sera inserida no indice 1: Test2
Digite o EMAIL da pessoa que sera inserida no indice 1: Email2
Digite a IDADE da pessoa que sera inserida no indice 1: 18
Digite o TELEFONE da pessoa que sera inserida no indice 1: 12345678912

NOME: Test1
EMAIL: Email1
IDADE: 19
TELEFONE: 1234567891

NOME: Test2
EMAIL: Email2
IDADE: 18
TELEFONE: -539222976

Solution

  • You are running into an int overflow. The second 'phone number' you typed in is larger than an int can store, and you are not verifying the reading. The scanf probably failed, and stored nothing in the int - which means there is still random bits in its memory area, as you never cleared it. The printf then prints those random information.

    You should always verify that scanf was successful - it will return the number of successfully assigned fields, here 1. So you should use `if (scanf(...) != 1) { /handle error/ }, instead of ignoring it.