c、去掉未初始化的警告错误

c, get rid of uninitialized warning error

在函数 my_func1() 中,我做的第一件事是调用另一个函数 my_func2(),它总是设置指针。 GCC 警告我可能未设置指针。我怎样才能摆脱警告?

这里有一些简化的代码,仅用于演示。

int bla;
void my_func2(int *ptr) {
   ptr = &bla;
}
void my_func1() {
    int *ptr;
    //ptr=0;
    my_func2(ptr);
}

如果行 ptr=0 未被注释,则警告消失。我不想设置变量,因为自从 my_func2() 设置它后它什么都不做。

gcc 警告信息是

warning: 'ptr' is used uninitialized in this function [-Wuninitialized]

如有任何帮助,我们将不胜感激。

我想你想做的是:

int bla;
void my_func2(int **pp) {
    *pp = &bla;
}
void my_func1() {
    int *ptr;
    my_func2(&ptr);
    ...
}