Someone could help me work struct with vectors and function, I’m not sure when to pass the data

Asked

Viewed 50 times

-1

typedef struct
{ int cod, num_hab;
  char sigla_esta[5], estado[30], cidade[30];
}Cidade;

main()
{
    //Variaveis 
    Cidade cidades[max];
    Estado estados[max];
    Pais paises[max];
    int op, i, posl, aux;
    int cc = 0, ce = 0, cp = 0; // um contador para cada vetor 
    char auxSt[50];
    //Funções Declaradas
    void CadCidade();
    
    
    
    do
    {   
        i=0;    //Zerando o I para nao gerar lixo da memoria depois de utilizar
        //Função menu
        menu(&op);

        switch(op){
            case 1: 
                
                CadCidade(&cc,&cidades);

The function is like this, but it’s not working

void CadCidade(int *ccF, Cidade *cid){
    
    //system("cls");    //Limpa a tela
    printf("\tInsira a cidade\n");
        
    printf("Digite o codigo da cidade: ");
    scanf("%d", &(*cid)[ccF].cod);
    fflush(stdin);

  • Like [ccF] is an index I believe should use [*ccF] has declared the parameter ccF pointer..

1 answer

0

  1. I believe Structs (types) Estado estados[max]; and Pais paises[max]; were not declared, as well as the Max variable, which should be declared and initialized before being used in Main, even if it is by a #DEFINE in the code header.

2.Function void CadCidade(); doesn’t need to be typed on Main

  1. I don’t understand the DO function used there, but as there is the comment on i below, I imagine it is used as a repetition, but since I don’t see the WHILE condition or stop condition, it may have an infinite loop.

4.Function menu(&op); no statement, if this is the statement, it needs to contain type of function.

  1. The use of scanf("%d", &(*cid)[ccF].cod); seems to me a wrong pointer access, because you request the address of the pointer variable, and not the content of it, which is what you want to access, I believe the correct syntax would be scanf("%d",&cid[ccf].cod);

Browser other questions tagged

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