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 ofagenda
when the object name isvet
?– Woss
@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
– Wilson Neto
Yes, but within the function you use
agenda[i].nome
, as ifagenda
were the variable, but it isvet
.– Woss
@Andersoncarloswoss what do I do then to refer to the variables stored in the array of agenda structs?
– Wilson Neto
Use the variable name
vet
.– Woss
@Andersoncarloswoss no match for 'Operator[]' (operand types are 'agenda' and 'int') gives this error; to almost giving up and taking out the program function
– Wilson Neto
Okay, take a good look at your code. You open the file
agenda.bin
and maintains the reference to it inarq
, and that’s the only place you use this variable. Shouldn’t you read data from that file? And where that variable was declared?– Woss