所以,我有一些代码,类似于下面的代码,可以将一个 struct 添加到一个 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
}
这些 struct 的定义如下:
typedef struct Bar
{
// this isn't too important
} Bar;
#define EMPTY_LIST NULL
typedef struct BarList
{
Bar * val;
struct BarList * nextBar;
} BarList;
然后在另一个文件中,我做了如下操作:
BarList * l;
l = EMPTY_LIST;
barPush(l,&b1); // b1 and b2 are just Bar's
barPush(l,&b2);
然而,在此之后,l仍然指向空的_列表,而不是barPush内部创建的修改版本.如果我想修改这个列表,我需要把它作为一个指向指针的指针传递进go 吗?还是需要一些其他的黑暗咒语?