我不明白爲什麼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;
}
[不投的malloc(朋友)的結果(http://stackoverflow.com/q/605845)。 – Deduplicator 2014-10-18 23:02:08
「對不起,我的代碼格式不正確」......爲什麼不在發佈之前更正它? – Deduplicator 2014-10-18 23:05:30
我刪除了劇組,但沒有幫助。如果格式不正確,我的意思是抱歉。我不認爲有問題。 – user2350896 2014-10-18 23:08:17