Dynamic allocation in wrong scope

Asked

Viewed 47 times

-1

I dynamically allotted a vector of structures with qtdeFuncionarios positions within a if, but now I need to use it within another conditional structure and the compiler accuses scope problem. How to proceed?

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

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

int main() {
  while(1){
    int i, opcao, qtdeFuncionarios;

    printf("1.Cadastrar funcionario\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("Quantos funcionarios deseja cadastrar? ");
        scanf("%d", &qtdeFuncionarios);
        Funcionario *funcionarios = malloc(qtdeFuncionarios * sizeof(Funcionario));
        for ( i = 0; i < qtdeFuncionarios; 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);
        }
    }   
    else if(opcao == 2){

    }    
    else if(opcao == 3){
        printf("Digite o numero do funcionario que deseja editar: ");
        int numero;
        scanf("%d", &numero);
        printf("Digite o novo nome: ");
        scanf("%34s", funcionarios[numero].nome);
        printf("Digite o novo email: ");
        scanf("%34s", funcionarios[numero].email);        
    }
  }
  return 0;
}
  • 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

Probably these lines shouldn’t be inside the if, should start up first and then do the maintenance work on array servant.

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

I put in the Github for future reference.

The error happens because this variable is inside a block that has a scope defined, nothing declared in there is worth out.

To tell the truth this code should not use dynamic allocation, it may be that it is some exercise ordering to do this, but if even the scope is so limited it is a waste to make this allocation, creating complexity where you do not need. And the exercise ends up teaching you to do something wrong with the case. It sounds like a naive mistake but my experience indicates that people do it once wrong and then they always do it.

I had arranged everything in the previous answer, it is sad to see that already started messing up again :(

Looking at this user’s other questions this answer is invalid. There is a requirement not described. You should not ask before how many employees will be registered, just start a declaration of the variable as null and allocate nothing, then make relocations as new employees are registered.

Browser other questions tagged

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