1
Good people, theoretically when we create a pointer and make it point to an array,
int a[10] = {0,1,2,3,4,5,6,7,8,9};
int *p = a;
then to increment the pointer to the next element we do something like
*(a+i)
But when it comes to a linked list, my code stops working. I have the following code
typedef struct lista carroPiloto, *pCarroPiloto;
struct lista {
piloto piloto;
carro carro;
int *tempo;
pCarroPiloto prox;
};
struct corrida {
int voltas; // entre 5 e 10
int comprimento; // entre 500 e 1000 (metros)
int n_carros; // numero maximo de carros a participar
};
/* ------------------ */
int tempos[caract.n_carros][caract.voltas];
pCarroPiloto aux = NULL;
for(j = 0; j < caract.voltas; j++) {
tempos[i][j] = calculaSegundos(idade,peso,exp,potencia,caract.comprimento); // matriz dos tempos
*(aux->tempo + j) = tempos[i][j];
}
And when I do this the pointer won’t keep the valuables. And I can’t do
aux = aux->prox
because thus advance to the next node and my goal is to have a pointer with multiple times within each node of the linked list.
There are several missing information in the question, such as the matrix definition
tempos
of the variablecarat
andaux
. But*(a+i)
does not increase the pointera
, simply return the value that is in a certain memory position froma
. In a concrete example*(a+3)
returns the value in the third memory position froma
considering the appropriate pointer arithmetic for the type ofa
.– Isac
@Isac I edited the post with the rest of the information. That’s right, my question is how do I pointer
aux->tempo
point to the next element whenever I read a matrix valuetempos[i][j]
– whoami
Yes exact. Missing this.
– whoami
"how do I make the aux->tempo pointer point to the next element" - Which element ? The next one in the list or matrix ? What if it is from the matrix which in particular is next ? The one from the same car on the next lap ?
– Isac
My goal is to go through the matrix times and go putting in
aux->tempo
the values it reads from the matrix. Yes it is from the same car, but from the next round.– whoami
So the idea was to put the
aux->tempo
to point to for example the first car first lap, thetempos[0][0]
and then with pointers pass to the time of the next turn of the same cartempos[0][1]
that’s it ?– Isac
Yeah, it was something like that. Like, when
tempos[0][0]
, then the first element to whereaux->lista
point will betempos[0][0]
and so on. When the cycle ends, it goes to the next pilot withaux = aux->prox
and do the same again– whoami