Question

Alors, j'ai un code, un peu comme ce qui suit, pour ajouter un struct à une liste de 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
}

Ces structures sont définies comme suit:

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

#define EMPTY_LIST NULL

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

puis dans un autre fichier que je fais quelque chose comme ce qui suit:

BarList * l;

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

Cependant, après cela, je souligne encore EMPTY_LIST, et non pas la version modifiée créée à l'intérieur de barPush. Est-ce que je dois passer la liste en tant que pointeur vers un pointeur si je veux le modifier, ou est-il une autre conjuration sombre nécessaire?

Était-ce utile?

La solution

Vous devez passer un pointeur vers un pointeur si vous voulez le faire.

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; 
}

Utilisez ensuite comme ceci:

BarList * l;

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

Jonathan Leffler a suggéré de retourner la nouvelle tête de liste dans les commentaires:

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; 
}

Utilisation devient:

BarList * l;

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

Autres conseils

Réponse générique:. Passez un pointeur vers la chose que vous voulez changer

Dans ce cas, il serait un pointeur vers le pointeur que vous souhaitez modifier.

Rappelez-vous, en C, tout est passé par valeur.

Vous passez un pointeur vers un pointeur, comme celui-ci

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

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

}

Oui, vous devez passer un pointeur vers le pointeur. C transmet des arguments par valeur, et non à titre de référence.

Ceci est un problème classique. Soit retourner le nœud alloué ou utiliser un pointeur de pointeur. En C, vous devez passer un pointeur sur un X à une fonction où vous voulez que votre X à modifier. Dans ce cas, puisque vous voulez un pointeur à modifier, vous devez passer un pointeur vers un pointeur.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top