Tak, mam kod, rodzaj jak poniżej, aby dodać struct do listy kodowanym:Jak zmodyfikować wskaźnik, który został przekazany do funkcji w C?
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
}
Struktury te są zdefiniowane w następujący sposób:
typedef struct Bar
{
// this isn't too important
} Bar;
#define EMPTY_LIST NULL
typedef struct BarList
{
Bar * val;
struct BarList * nextBar;
} BarList;
a następnie w kolejnym złożyć zrobić coś jak następuje:
BarList * l;
l = EMPTY_LIST;
barPush(l,&b1); // b1 and b2 are just Bar's
barPush(l,&b2);
jednak po tym, ja nadal wskazuje EMPTY_LIST nie zmodyfikowana wersja stworzona wewnątrz barPush. Czy muszę przekazać listę jako wskaźnik do wskaźnika, jeśli chcę go zmodyfikować, czy też jest wymagana inna ciemna inkantacja?
Dzięki, doszedłem do wniosku, że to był problem, ale miałem nadzieję, że tak się nie stało;) –
Alternatywnie, funkcja powinna zwrócić wskaźnik do nowej głowy listy. BarList * barPush (lista BarList *, Bar * bar) –