Increment pointer inside C-linked list

Asked

Viewed 137 times

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 variable carat and aux. But *(a+i) does not increase the pointer a, simply return the value that is in a certain memory position from a. In a concrete example *(a+3) returns the value in the third memory position from a considering the appropriate pointer arithmetic for the type of a.

  • @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 value tempos[i][j]

  • Yes exact. Missing this.

  • "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 ?

  • 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.

  • So the idea was to put the aux->tempo to point to for example the first car first lap, the tempos[0][0] and then with pointers pass to the time of the next turn of the same car tempos[0][1] that’s it ?

  • Yeah, it was something like that. Like, when tempos[0][0], then the first element to where aux->lista point will be tempos[0][0] and so on. When the cycle ends, it goes to the next pilot with aux = aux->prox and do the same again

Show 2 more comments

1 answer

1


(...) my goal is to have a multi-stroke pointer within each linked list node

A pointer only tip to a memory zone, which leads to the first problem: Who defined this memory zone and put the values there ?

Here are two solutions:

  • Have a pointer to the matrix tempos already in the correct element, and each time you want to navigate the matrix through the pointer you have to know how to do it.

    In this scenario I would start by manually assigning address of the first element of the appropriate matrix to the pilot.

    pCarroPiloto aux = malloc(sizeof(carroPiloto));
    
    aux->tempo = &(tempos[0][0]); //primeiro piloto aponta para o [0][0]
    

    And this puts his time pointer pointing to the address of tempos[0][0]. With this pointer you can always traverse all the elements of the matrix that matter. To traverse you only need to do the following:

    int *tempo_corrente = aux->tempo; //declarar outro ponteiro que aponta para o inicio das voltas
    for(j = 0; j < caract.voltas; j++) {
        printf("%d\n", *tempo_corrente); //utilizar tempo corrente
        tempo_corrente = tempo_corrente + caract.voltas; //passar para o próximo com aritmética de ponteiro
    }
    

    An array is stored as a contiguous zone of memory and each row is stored one after the other. So to take a pointer and move to the next line of the same matrix just advance the amount of elements that exist on a line, which in your example is caract.voltas.

    Note that you should not do aux->tempo = aux->tempo + caract.voltas else lose the reference for the first time of that car.

  • Have a pointer to a memory zone defined by you, and built through malloc in which to replicate matrix values tempos in that new memory.

    In this solution the first step is to allocate space for the various car turns:

    pCarroPiloto aux = malloc(sizeof(carroPiloto));
    
    aux->tempo = malloc(sizeof(int) * caract.voltas);
    

    Now you can go through the matrix tempos and keep the values that matter:

    for(j = 0; j < caract.voltas; j++) {
        tempos[i][j] = calculaSegundos(idade,peso,exp,potencia,caract.comprimento);     
        aux->tempo[j] = tempos[i][j];
    }
    

    But where is the pointer notation ?

    Well you can use pointer notation if you want in this case, but it is totally unnecessary and complicates, but it would look the same as the question:

    for(j = 0; j < caract.voltas; j++) {
        tempos[i][j] = calculaSegundos(idade,peso,exp,potencia,caract.comprimento);     
        *(aux->tempo + j) = tempos[i][j];
    }
    

    However the biggest difference to your code is in the malloc I gave to the pointer tempo, because if you don’t, you’re trying to store elements in a pointer that doesn’t point to a location defined by you, and that represents undefined behavior, and a potential Segmentation fault.

    The same can be said for the variable aux which cannot have access to its fields if it is declared as NULL.

Browser other questions tagged

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