所以,我有一些代码,有点像下面的结构增加结构的列表:如何修改已传递到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
}
这些结构的定义如下:
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仍然指向EMPTY_LIST,不barPush内创建的修改后的版本。如果我想修改它,还是需要其他一些黑暗咒语,我是否必须将列表作为指针传入指针?
谢谢,我想这是问题所在,但希望它不是;) – 2009-04-20 04:38:52