這裏是我的代碼:作爲賦值左操作數所需的左值?
void pwd(Fs_sim *files) {
Node *curr_node = files->curr;
Node **space = NULL;
int i = 0;
while (curr_node->parent != NULL) {
space = realloc(space, sizeof(Node *) * (i + 1));
*space + i = curr_node;
i++;
curr_node = curr_node->parent;
}
if (i == 0)
printf("/\n");
else {
while (i > 0) {
printf("/%s", (*space + i--)->name);
}
printf("\n");
}
free(space);
}
「空間」是一個指向就是BEING動態分配的數組。當每個節點被迭代時,我想要在動態分配的數組中存儲一個指向該節點的指針,並且保留一個有多少元素的計數。我收到錯誤消息:'* space + i = curr_node'上的錯誤消息:
error: lvalue required as left operand of an assignment
線。
我沒有看到它有什麼問題。有人可以澄清嗎?
UPDATE:
我已經改變了代碼,它現在編譯,但我得到分段錯誤當我運行可執行文件。這裏是我的代碼:
void pwd(Fs_sim *files) {
Node *curr_node = files->curr;
Node **space = NULL;
int i = 0;
while (curr_node->parent != NULL) {
space = realloc(space, sizeof(Node *) * (i + 1));
*(space + i) = curr_node;
i++;
curr_node = curr_node->parent;
}
if (i == 0)
printf("/\n");
else {
while (i >= 0) {
printf("/%s", (*(space + (i-1)))->name);
i--;
}
printf("\n");
}
free(space);
}
仍然無法找到它的問題。
提前致謝。在這裏大C小菜。
我想你的意思是'*(space + i)= curr_node;' –
@VaughnCato:'*(space + i)'最好寫成'space [i]'。 –