2011-12-20 205 views
0

我有以下問題,下面的函數被調用時衝突被初始化爲NULL。傳遞指針數組的指針作爲參數

在foo結尾處,衝突採用正確的值。在這個例子中,*衝突應該包含值4,8和2.根據fprintfs,它會做什麼。

但是,當我在調用foo的函數中再次測試時(請參閱第二段代碼摘錄),衝突數組未被修改。我不知道爲什麼,因爲我傳遞了一個指向數組的指針,特別是因爲這種技術適用於multiDB和遞歸指針。任何幫助,將不勝感激。 (這不是完整的代碼,我只顯示了相關的部分)。謝謝!

int foo(
    /*==================*/ 
    uchar* buf, 
    uint* multiDB, 
    uint* recursive, 
    uint** conflict) { 

select_consistent= conflict; 
bool finished; 

if (((start_of_scan == 1) && (*multiDB != 1)) || ((start_of_scan== 0) && (select_consistent == NULL))) { 
    fprintf(stderr, "Not doing select consistent \n "); 

    finished = TRUE; 
} 
else { 
    *multiDB=0; 
    fprintf(stderr, "Doing select consistent \n "); 
    finished = FALSE; 
    uint n; 
    int i = 0 ; 
    if (select_consistent == NULL) { /*This is the first round */ 
      next_round = FALSE; 
     fp = popen("./algorithm '[t(1,[t(2,||),t(3,[t(8,||),t(10,||)])]).t(1,[t(4,||),t(6,||)]).t(1,[t(2,||),t(7,||)])]'", "r"); /* Issue the command.  */ 
     finished = FALSE; 
    } 
    if (next_round == TRUE) { 
     goto parse_records; 
    } 

     fscanf(fp, "%lu", &n); 
     uint* conflict_ ; 
     if (n!=0) conflict_ = (uint*) malloc(sizeof (uint) * n); 
     conflict = &conflict_; 
     next_round = TRUE; 
     int j= 0 ; 
     while (fscanf(fp, "%lu", &n) != EOF) { 
      if (n == 0) { 
       select_consistent=conflict; 
       goto parse_records; 
      } 
      else { 
       (*conflict)[j] = n; 
      } 
      i++; 
      j++; 

     } 
     finished = TRUE;  
} 
parse_records:; 
int error; 

.... [other code] foo2(multiDB, recursive); 

fprintf(stderr, "Array states %lu %lu \n ", (*conflict)[0], (*conflict)[1]); 
fprintf(stderr, "Array states %lu %lu \n ", (*select_consistent)[0], (*select_consistent)[1]); 



return error 
} 

函數調用foo確實是這樣的:

uint** conflict = NULL ; 
    error = foo(buf, multiDB, recursive, conflict); 
    fprintf(stderr, "value is %lu \n", (*conflict)[0]); //This is still unitialised. 

回答

0

看來,代碼中有一些間接指針的問題。看來意味着是uint的一維數組。如果是這樣,那麼最初的聲明應該是:

uint* conflict = NULL; 

然後通過這樣的:

foo(... &conflict); 

然後在foo如下分配它:

*conflict = (uint*)malloc(...); 

或者,如果你仍然想使用conflict_,像這樣指定:

*conflict = conflict_;