Passing from struct to function giving error

2

I am finalizing a phone book and the program compiles almost integer, except in the function with the ordering method for the names. In the following code snippet, the compiler detects an error when I try to play struct to the temporary variables, which will make the change of position if necessary and the same error it indicates in all the excerpts in which I try to use the data of struct . The error message says "expected primary expression before ['token." And it's the same message everywhere I try to access struct

Excerpt from function:

   typedef struct agenda{
   char nome[30];
   int numero;
   }agenda;

   void ordenar(void)
   {
    agenda vet;
    int aux=1000, i, j, k, retorno;
    char *str, *str2, *straux;

        arq = fopen("agenda.bin", "a+b");
        for (i = 0; i < aux; i++)
        {
            str = agenda[i].nome;
            for (j = i + 1; j < aux; j++)
            {
                str2 = agenda[j].nome;
                if (strcmp(str, str2) > 0)
                {
                    vet = agenda[i];
                    agenda[i] = agenda[j];
                    agenda[j] = vet;
                }
            }
        }
    
asked by anonymous 05.06.2017 / 21:07

2 answers

1

I see some errors:

1) You have not declared the pointer to file arq:

FILE *arq

2) Schedule is the name of its type, not a variable. Just as it does not make sense to type int [i], it's the same as typing agenda [i] as it is in:

str = agenda[i].nome;

3) Your statement that is given by

agenda vet;

It has only one element, it's like "int my_number;". It is not a vector and so you can not use "vet [i]" because vet is a calendar type variable and not a calendar type vector.

4) Besides you do not declare the vector, even if you did, your elements are out of the mess. So, it does not make sense for you to type the code below, which is similar to yours:

void minha_funcao()
{
    int meu_vetor[2];
    int aux;
    if(meu_vetor[0] > meu_vetor[1])
    {
        aux = meu_vetor[0];
        meu_vetor[0] = meu_vetor[1]
        meu_vetor[1] = aux;
    } 
}

In this case, you are comparing two values that were not declared at startup.

    
06.06.2017 / 00:31
0

What you would have liked to have done was:

void ordenar(agenda *vet) {
    /* apenas o código da ordenação aqui, sem leitura de arquivo; essa função deve apenas ordenar o vetor */

    ...
}

agenda *leitura_agendas_arquivo(char *nome_arquivo) {
    /* faça a leitura das estruturas aqui */

    ...
}

agenda *le_ordena_agendas() {
    agenda *vet = leitura_agenda_arquivo("agenda.bin");
    ordena(vet);

    return agenda;
}

Do not forget to fill in the blanks !!

Doing this correctly, all four notes from the Carlos Adir answer will be healed.

    
06.06.2017 / 11:55