未初始化的局部变量

uninitialized local variable

本文关键字:局部变量 初始化      更新时间:2023-10-16

我第一次尝试在visualstudio上编译c++,但遇到了一个错误。

/***Setup****/
struct id_priority{
    int id;
    int priority;
};
struct temp_heap{
    int id;
    int priority;
};
/**heapify up**/
void heapify(id_priority heap[], int index, int length, temp_heap temp){
}
int main(){
    int *command_processed;
    command_processed = new int[6];
    id_priority *heap;
    heap = new id_priority[1000];
    temp_heap temp;
    int index = 0;
    int length = 0;
    heapify(heap, index, length, temp);
    return (0);
}

heapify(堆,索引,长度,临时);这一行说使用了未初始化的局部变量"temp",但这段代码在Linux上的终端上运行良好。我不知道这里出了什么问题。

我们不知道应该将temp初始化为什么。但这里有两种方法可以初始化它。我假设您希望它的字段都为0。

    temp_heap temp = {0, 0};

    temp_heap temp;
    temp.id = 0; temp.priority=0;