<分区>
我有一个问题,这听起来很愚蠢,但我就是不明白。我尝试使用以下结构为列表编写一个小 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;
我可以在调试器中看到,start 的值仍然是来自 main-Method 的 NULL。这两个指针对我来说似乎是同一类型,我没有收到警告或任何带有此编译器标签的内容
-Wall -ansi -Wconversion -pedantic -m64
我真的不明白,我做错了什么。