Come faccio a modificare un puntatore che è stato passato in una funzione in C?
-
12-09-2019 - |
Domanda
Quindi, ho qualche codice, un po 'come il seguente, per aggiungere una struttura a una lista di struct:
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
}
Queste strutture sono definiti come segue:
typedef struct Bar
{
// this isn't too important
} Bar;
#define EMPTY_LIST NULL
typedef struct BarList
{
Bar * val;
struct BarList * nextBar;
} BarList;
e poi in un altro file che faccio qualcosa di simile al seguente:
BarList * l;
l = EMPTY_LIST;
barPush(l,&b1); // b1 and b2 are just Bar's
barPush(l,&b2);
Tuttavia, dopo questo, l punta ancora EMPTY_LIST, non la versione modificata creata all'interno di barPush. Devo passare lista come un puntatore a un puntatore se voglio modificarlo, o c'è qualche altro incantesimo scuro richiesta?
Soluzione
È necessario passare in un puntatore a un puntatore se si vuole fare questo.
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;
}
Quindi utilizzare in questo modo:
BarList * l;
l = EMPTY_LIST;
barPush(&l,&b1); // b1 and b2 are just Bar's
barPush(&l,&b2);
Jonathan Leffler ha suggerito di tornare il nuovo capo della lista nei commenti:
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;
}
Utilizzo diventa:
BarList * l;
l = EMPTY_LIST;
l = barPush(l,&b1); // b1 and b2 are just Bar's
l = barPush(l,&b2);
Altri suggerimenti
risposta generica:. Passa un puntatore alla cosa che si desidera modificare
In questo caso, sarebbe un puntatore al puntatore che si desidera modificare.
Ricordate, in C, tutto è passato per valore.
Si passa in un puntatore a un puntatore, come questo
int myFunction(int** param1, int** param2) {
// now I can change the ACTUAL pointer - kind of like passing a pointer by reference
}
Sì, si deve passare in un puntatore al puntatore. C passa gli argomenti per valore, non per riferimento.
Questo è un problema classico. O restituire il nodo assegnato o utilizzare un puntatore di puntatore. In C, si dovrebbe passare un puntatore a un X a una funzione in cui si desidera che il X da modificare. In questo caso, dal momento che si desidera un puntatore da modificare, si dovrebbe passare un puntatore a un puntatore.