我差不多完成了我的代码,但它给了我这个错误:'curtemp: undeclared identifier
'。和'prevtemp: undeclared identifier
'和'missing ';' before type
'(最后一个错误出现在“float curtemp = current-> temp;”。我不知道我的代码有什么问题。我正在尝试当元素内部的温度比前一个元素的温度高5或5时,从双链表中删除一些元素。
这是我的.c文件:
void remove_error(Dlist *list){
DlistElmt *current;
current = list->head;
//initializes ints for comparison
float curtemp = current->temp;
float prevtemp = current->temp;
//now moves current to next data
current = current -> next;
//begins while loop for comparison of data
while(current != NULL){
curtemp = current -> temp;
if((curtemp >= (prevtemp +5)) || (curtemp <= (prevtemp-5))){
//removes current from the list
dlist_remove(list, current);
current = current->next;
}
}
}
这是我的struct元素文件:
typedef struct DlistElmt_ {
int hour;
int min;
float temp;
struct DlistElmt_ *prev;
struct DlistElmt_ *next;
} DlistElmt;
答案 0 :(得分:0)
对于C89或C90,预计变量必须在块的开头(函数或本地块)声明。但是,对于C99,此限制不适用。因此,很可能,Karthik T&amp; S的建议。上面的japreiss必须适用。
您可以尝试将float变量的声明仅移动到函数的开头,稍后再分配它们。
答案 1 :(得分:0)
您的代码不是ISO标准C89,我怀疑您有一个C89编译器(如Visual C)。你必须重新编码一些行:
DlistElmt *current;
float curtemp, prevtem;
current = list->head;
//initializes ints for comparison
curtemp = prevtemp = current->temp;
这样做。
它是gcc
C89扩展,C99标准,以及C ++功能,允许在块的中间而不是仅在顶部进行声明。当遇到“纯粹的”ISO C89编译器时,这会让许多人感到困惑。
在您的逻辑中,它看起来也像缺少一些东西。设置后prevtemp
永远不会更新。如果prev
表示之前,我猜你想要在循环中:
prevtem = curtemp;
curtemp = current -> temp;
在这种情况下,您的一些初始化步骤是多余的。考虑阅读“编程科学”中的Gries来学习如何避免这些类型的错误。
答案 2 :(得分:0)
我的猜测是你没有包含文件 DlistElmt ,但你已经包含了DlistElmt的声明,所以编译器知道有DlistElmt的东西,所以你可以使用指向它的指针,但是当你尝试用DlistElmt的内容做某事时,编译器无法对它做任何事情。