2011-12-20 254 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_;