Pointer accessed improper data

Asked

Viewed 59 times

-2

I’m having difficulty implementing the logic of introducing new employees in the code below. When determining the new quantity and reallocating the vector based on it, the first new employee is showing up with the advanced or delayed position or the program is ending

como fica a execução.

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

typedef struct {
char *nome;
char *email;
int idade;
} Funcionario;

int main() {

int i, opcao, qtdeInicialFuncionarios, novaQtdeFuncionarios;

printf("Quantos funcionarios deseja cadastrar? ");
scanf("%d", &qtdeInicialFuncionarios);
Funcionario *funcionarios = malloc(qtdeInicialFuncionarios* sizeof(Funcionario));

for ( i = 0; i < qtdeInicialFuncionarios; i++) {  

    funcionarios[i].nome = malloc(35);
    printf("\nDigite o nome do funcionario %d: ", i);
    scanf("%34s", funcionarios[i].nome);
    printf("\n%s", funcionarios[i].nome);
    funcionarios[i].email = malloc(35);
    printf("\nDigite o email do funcionario %d: ", i);
    scanf("%34s", funcionarios[i].email);
    printf("\n%s", funcionarios[i].email);
    printf("\nDigite a idade do funcionario %d: ", i);
    scanf("%d", &funcionarios[i].idade);
    printf("\n%d", funcionarios[i].idade);
}

while(1){

    printf("1.Cadastrar mais funcionarios\n");
    printf("2.Listar funcionarios\n");
    printf("3.Editar dados de funcionario\n");
    printf("4.Excluir funcionario\n");
    scanf("%d", &opcao);

    if (opcao == 1) {

        printf("Deseja cadastrar mais quantos funcionarios? ");
        scanf("%d", &novaQtdeFuncionarios);


        funcionarios = (Funcionario*)realloc(funcionarios, novaQtdeFuncionarios* sizeof(Funcionario));

        for ( i = qtdeInicialFuncionarios  ; i <= novaQtdeFuncionarios ; i++) {  

            funcionarios[i].nome = malloc(35);
            printf("\nDigite o nome do funcionario %d: ", i);
            scanf("%34s", funcionarios[i].nome);
            printf("\n%s", funcionarios[i].nome);
            funcionarios[i].email = malloc(35);
            printf("\nDigite o email do funcionario %d: ", i);
            scanf("%34s", funcionarios[i].email);
            printf("\n%s", funcionarios[i].email);
            printf("\nDigite a idade do funcionario %d: ", i);
            scanf("%d", &funcionarios[i].idade);
            printf("\n%d", funcionarios[i].idade);
        }
        qtdeInicialFuncionarios = (qtdeInicialFuncionarios + novaQtdeFuncionarios)-1;

    }




    else if(opcao == 3){

        printf("Digite o numero do funcionario que deseja editar: ");
        int numero;
        scanf("%d", &numero);
        printf("Digite o novo nome do funcionario %d: ", numero);
        scanf("%34s", funcionarios[numero].nome);
        printf("Digite o novo email do funcionario %d: ", numero);
        scanf("%34s", funcionarios[numero].email);
        printf("Digite a nova idade do funcionario %d: ", numero);
        scanf("%d\n", &funcionarios[numero].idade);

    }


}


return 0;
}

That is the statement:

inserir a descrição da imagem aqui

  • From what I understand if you want to register more x employees then that reallocate memory "adding" more x positions to your already allocated area and not as you did where you just realoca x positions without considering what has already been previously allocated.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

This is getting worse. First it was to choose how many employees would register, now you’re asking to add new employees? Where did this requirement come from that I didn’t have before? If this is what to do then why ask to register before? I would review all this.

With editing it is clear that it had this requirement, so it makes no sense to start the application by asking employees. Anyway I never answered that I should do this even without knowing this requirement. I edited the previous question to reflect the requirements posted here.

The big problem is that you don’t seem to understand anything that’s going on in the code, and this indicates that you’re not learning, or you’re doing something more complex than you’re prepared for now. He even made mistakes I’d already made before.

In the condition can not only go up to the new amount of employees, have to consider the amount of employees already registered before also, in certain quantities nor would do anything because the number that is finalizing is lower than the initial number. Interestingly it did right when changing the total value of employees, although it could have been written more simply. But again, review if you’re really going to do this. And decide what you have to do. If you start doing random things, any solution will be wrong. Something like that:

for (int i = 0; i < qtdeInicialFuncionarios + novaQtdeFuncionarios; i++) {

I put in the Github for future reference.

  • The requirement was always there, only it had not been made explicit in the previous questions. I’m sharing the solution based on an overview of the problem.

  • No correct requirements give you no correct answers. All your questions are wrong because you had requirements and your code didn’t follow them, so you made all your answers wrong too. Especially the previous question completely lost its meaning.

Browser other questions tagged

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