Pregunta

Por lo tanto, tengo algo de código, algo así como lo siguiente, para agregar una estructura a una lista de estructuras:

void barPush(BarList * list,Bar * bar)
{
    // if there is no move to add, then we are done
    if (bar == NULL) return;//EMPTY_LIST;

    // allocate space for the new node
    BarList * newNode = malloc(sizeof(BarList));

    // assign the right values
    newNode->val = bar;
    newNode->nextBar = list;

    // and set list to be equal to the new head of the list
    list = newNode; // This line works, but list only changes inside of this function
}

Estas estructuras se definen como sigue:

typedef struct Bar
{
    // this isn't too important
} Bar;

#define EMPTY_LIST NULL

typedef struct BarList
{
    Bar * val;
    struct  BarList * nextBar;
} BarList;

y luego en otro archivo que hago algo como lo siguiente:

BarList * l;

l = EMPTY_LIST;
barPush(l,&b1); // b1 and b2 are just Bar's
barPush(l,&b2);

Sin embargo, después de esto, l todavía señala a EMPTY_LIST, no la versión modificada creada dentro de barPush. ¿Tengo que pasar lista en como un puntero a un puntero si quiero modificarlo, o hay alguna otra encantamiento oscuro requerido?

¿Fue útil?

Solución

Tiene que pasar un puntero a un puntero si usted quiere hacer esto.

void barPush(BarList ** list,Bar * bar)
{
    if (list == NULL) return; // need to pass in the pointer to your pointer to your list.

    // if there is no move to add, then we are done
    if (bar == NULL) return;

    // allocate space for the new node
    BarList * newNode = malloc(sizeof(BarList));

    // assign the right values
    newNode->val = bar;
    newNode->nextBar = *list;

    // and set the contents of the pointer to the pointer to the head of the list 
    // (ie: the pointer the the head of the list) to the new node.
    *list = newNode; 
}

A continuación, utilizar de esta manera:

BarList * l;

l = EMPTY_LIST;
barPush(&l,&b1); // b1 and b2 are just Bar's
barPush(&l,&b2);

Jonathan Leffler sugirió devolver la nueva cabeza de lista en los comentarios:

BarList *barPush(BarList *list,Bar *bar)
{
    // if there is no move to add, then we are done - return unmodified list.
    if (bar == NULL) return list;  

    // allocate space for the new node
    BarList * newNode = malloc(sizeof(BarList));

    // assign the right values
    newNode->val = bar;
    newNode->nextBar = list;

    // return the new head of the list.
    return newNode; 
}

Uso convierte en:

BarList * l;

l = EMPTY_LIST;
l = barPush(l,&b1); // b1 and b2 are just Bar's
l = barPush(l,&b2);

Otros consejos

respuesta genérica:. Pasar un puntero a lo que desea cambiar

En este caso, sería un puntero al puntero que desea cambiar.

Recuerde, en C, todo se pasa por valor.

Se pasa un puntero a un puntero, como este

int myFunction(int** param1, int** param2) {

// now I can change the ACTUAL pointer - kind of like passing a pointer by reference 

}

Sí, usted tiene que pasar en un puntero al puntero. C pasa argumentos por valor, no por referencia.

Este es un problema clásico. O bien devolver el nodo asignado o utilizar un puntero de puntero. En C, debe pasar un puntero a una X a una función en la que desea que su X para ser modificado. En este caso, ya que desea un puntero a ser modificado, usted debe pasar un puntero a un puntero.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top