2014-10-18 99 views
-2

我不明白爲什麼if語句:& * accountslist [1] == & * unameslist [0]爲true。這裏發生了什麼?我的realloc有問題嗎?另外,當我讓accountlist [1] = NULL時,似乎unameslist [0]也被設置爲null。這使我認爲,由於某種原因,他們最終指向同一地點。指針問題,陣列第二個位置與另一個陣列中的第一個位置相同

int loadInfo(char*** accountslist, 
      char*** unameslist, 
      char*** passlist) 
{ 
    FILE* account_file = fopen(ACCOUNTS_LOCATION, "r"); 
    FILE* uname_file = fopen(UNAMES_LOCATION, "r"); 
    FILE* pass_file = fopen(PASSWORDS_LOCATION, "r"); 

    char* nextAccount; 
    char* nextUname; 
    char* nextPass; 

    fileWithNum(account_file, &nextAccount); 
    fileWithNum(uname_file, &nextUname); 
    fileWithNum(pass_file, &nextPass); 

    *accountslist = (char**) malloc(sizeof(char*)); 
    *unameslist = (char**) malloc(sizeof(char*)); 
    *passlist = (char**) malloc(sizeof(char*)); 

    *accountslist[0] = nextAccount; 
    *unameslist[0] = nextUname; 
    *passlist[0] = nextPass; 

    int num_accounts = 1; 

    while(nextAccount != NULL && nextUname != NULL && 
      nextPass != NULL) 
    { 
     *accountslist = (char**)realloc(*accountslist, 
           (num_accounts + 1) * sizeof(char*)); 
     *unameslist = (char**)realloc(*unameslist, 
           (num_accounts + 1) * sizeof(char*)); 
     *passlist = (char**)realloc(*passlist, 
          (num_accounts + 1) * sizeof(char*)); 
     fileWithNum(account_file, &nextAccount); 
     fileWithNum(uname_file, &nextUname); 
     fileWithNum(pass_file, &nextPass); 
     printf("%s\n" , *unameslist[0]); 
     *accountslist[1] = NULL; 
     if(&*accountslist[1] == &*unameslist[0]) printf("WHY??\n"); 
     printf("%s, %s, %s\n" , *accountslist[0], *unameslist[0], *passlist[0]); 

     *accountslist[num_accounts] = nextAccount; 
     *unameslist[num_accounts] = nextUname; 
     *passlist[num_accounts] = nextPass; 

     printf("%s, %s, %s\n" , *accountslist[num_accounts], 
      *unameslist[num_accounts], *passlist[num_accounts]); 
     num_accounts++; 
    } 

    fclose(account_file); 
    fclose(uname_file); 
    fclose(pass_file); 

    return num_accounts; 
} 
+0

[不投的malloc(朋友)的結果(http://stackoverflow.com/q/605845)。 – Deduplicator 2014-10-18 23:02:08

+2

「對不起,我的代碼格式不正確」......爲什麼不在發佈之前更正它? – Deduplicator 2014-10-18 23:05:30

+0

我刪除了劇組,但沒有幫助。如果格式不正確,我的意思是抱歉。我不認爲有問題。 – user2350896 2014-10-18 23:08:17

回答

0

&*accountslist[1]相當於accountslist[1]&*unameslist[0]相當於unameslist[0],所以你可以重寫路線爲:

if(accountslist[1] == unameslist[0]) printf("WHY??\n"); 

的問題是,無論是accountslistunameslist是指向你的列表,即他們是char ***類型。他們本身並不是陣列,因此unameslist[0]等於*unameslist這是您的實際unameslist陣列的地址,而accountslist[1]實際上是超出範圍。既然這會讓你陷入未定義的行爲領域,那麼你就不能指望事物的行爲合理。可能發生的情況是,在調用loadInfo()之前,您宣佈unameslist指向accountslist指向的那個點之前,因此accountslist[1]實際上的計算結果與*unameslist的地址相同。這個簡短的程序演示了這種情況發生:

#include <stdio.h> 

void myfunc(char *** pp1, char *** pp2) 
{ 
    /* WARNING - undefined behavior ahead */ 

    printf("pp1[1] is %p, pp2[0] is %p\n", (void *) pp1[1], (void *) pp2[0]); 
} 

int main(void) 
{ 
    char ** p1 = (char **) 0xCC; 
    char ** p2 = (char **) 0xFF; 
    printf("p1 is %p, p2 is %p\n", (void *) p1, (void *) p2); 
    printf("&p1 is %p, &p2 is %p\n", (void *) &p1, (void *) &p2); 
    myfunc(&p2, &p1); 
    return 0; 
} 

其中,至少當我跑了,輸出:

[email protected]:~/src/sandbox$ ./pdemo 
p1 is 0xcc, p2 is 0xff 
&p1 is 0x7fff8d138818, &p2 is 0x7fff8d138810 
pp1[1] is 0xcc, pp2[0] is 0xcc 
[email protected]:~/src/sandbox$ 

,你可以看到如何pp1[1]pp2[0]是比較平等的,由於濫用指針。如果您像以前一樣將pp1[1]更改爲&*pp1[1],並且同樣對pp2[0]進行更改,則會看到您獲得完全相同的結果,因爲它們是相同的。

你真正的意思大概是這樣的:

if((*accountslist)[1] == (*unameslist)[0]) printf("WHY??\n"); 

注意[]結合更加緊密比*呢,所以*accountslist[1]相當於*(accountslist[1]),當你真正想要的是尊重accountslist第一,所以你需要(*accountslist)[1]

這裏的簡短答案是,你有太多的間接性,你會迷惑自己,極大地濫用你的指針。因此,幾乎沒有任何代碼正在執行您認爲正在執行的操作。你應該使用一些臨時變量來簡化並使其對自己更容易,如:

char ** local_accounts_list = malloc(sizeof *local_accounts_list); 

,然後只在最後更新的參數有:

*accountslist = local_accounts_list; 

您也應該檢查從收益malloc()和朋友,fopen(),以及其他可能會失敗的功能,這是你永遠不會做的。如果您不檢查錯誤,那麼當事情不起作用時您不會感到驚訝。

+0

這個問題最終導致嘗試使用傳入的變量而不是臨時變量,然後將其分配給最後的參數。謝謝您的幫助。 – user2350896 2014-10-23 23:17:04

0

這裏是fileWithNum如果有人有興趣

int fileWithNum(FILE* fptr, char** word) 
{ 
    //finished fixing 
    int size; 
    char* temp = NULL; 
    fscanf(fptr, "%d", &size); 
    if(size == -1) 
    { 
    return 0; 
    } 
    int character = 0; 
    temp = (char*) malloc((size + 1) * sizeof(char)); 
    int current; 
    for(current = 0; current < size; current++) 
    { 
     if(fscanf(fptr, "%d", &character) != 1) 
     { 
      printf("fscanf error\n"); 
     } 
     //printf("%c\n" , character); 
     //printf("%d\n" , current); 
     temp[current] = character; 
    } 
    temp[size] = 0; //end of string char 
    *word = temp; 
    printf("End of filewithnum\n"); 
    return 0; 
} 
相關問題