在c中的函数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);
...
}
关于c、摆脱未初始化的警告错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47720027/