我有一個問題,這聽起來很蠢,但我只是不明白。我嘗試使用下面的結構爲列表編程一個小的shell:結構上的指針無法複製到函數內的另一個指針
struct shellvalue {
char* word;
char isorder;
char isarg;
char ispipe;
char isamp;
unsigned int wordcount;
unsigned int ordercount;
struct shellvalue* next;
};
我開始在主法兩個指針與
struct shellvalue* start;
struct shellvalue* current;
然後我分配現在內存的第一個元素:
void addtoken(char* word, int counter, struct shellvalue* start,
struct shellvalue* current)
{
if (counter == 0)
{ //creating first element
size_t structsize = sizeof(struct shellvalue);
struct shellvalue* tmp = (struct shellvalue*) malloc(structsize);
tmp->word = malloc(strlen(word) + 1);
strcpy(tmp->word, word);
start = tmp;
start->next = NULL;
current = start;
}
else
{ // append new element
struct shellvalue* new = (struct shellvalue*) malloc(
sizeof(struct shellvalue));
new->word = malloc(strlen(word) + 1);
strcpy(new->word, word);
current->next = new;
current = new;
}
}
但是,當我嘗試做
start = tmp;
我可以在調試器中看到,該啓動仍然具有來自main-Method的NULL值。兩個指針似乎是同一類的我,我得不到warnigs或任何與此compilertags
-Wall -ansi -Wconversion -pedantic -m64
我真的不知道,我做錯了。
C通過值傳遞。函數內部的'start'只是傳遞給調用它的函數的**拷貝**。 – alk
1+使用調試器! :-) – alk