Passing struct to function giving error

Asked

Viewed 169 times

2

I am finalizing a phone book and the program compiles almost the entire, except in the function with the method of ordering the names. In the following code snippet, the compiler detects error the moment I try to play the struct for the temporary variables, which will exchange position if necessary and the same error it indicates in all the passages where I try to use the data of the struct. The error message says "expected Primary Expression before'[' token." And it’s the same message in every snippet that I try to access struct

Part of the 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;
                }
            }
        }
  • What is the definition of struct and what is the error message? Please [Edit] the question and add this information. And why are you accessing the positions of agenda when the object name is vet?

  • @Andersoncarloswoss edited the question, I think it’s right now; and vet is only a variable of the type agenda, to make the position change

  • Yes, but within the function you use agenda[i].nome, as if agenda were the variable, but it is vet.

  • @Andersoncarloswoss what do I do then to refer to the variables stored in the array of agenda structs?

  • Use the variable name vet.

  • @Andersoncarloswoss no match for 'Operator[]' (operand types are 'agenda' and 'int') gives this error; to almost giving up and taking out the program function

  • Okay, take a good look at your code. You open the file agenda.bin and maintains the reference to it in arq, and that’s the only place you use this variable. Shouldn’t you read data from that file? And where that variable was declared?

Show 2 more comments

2 answers

1

I see some mistakes:

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

FILE *arq

2) agenda is the name of its type, not a variable. Just as it makes no sense for you to type int[i], it is the same as typing agenda[i] as it is in:

str = agenda[i].nome;

3) Your statement which is given by

agenda vet;

It has only one element, it’s like "int meu_numero;". It is not a vector and so you cannot use "vet[i]", because vet is an agenda type variable and not an agenda type vector.

4) In addition to you not declaring the vector, even if you had, your elements are of the mess. Thus, it makes no sense for you to enter the code below, which is similar to your:

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 have not been declared at first.

0

What you wish you had 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;
}

Don’t forget to fill in the gaps!!

Doing this correctly, all four notes from reply by Carlos Adir will be cured.

Browser other questions tagged

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