所以,我有一些代碼,有點像下面的結構增加結構的列表:如何修改已傳遞到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