In function my_func1()
in c the 1st thing I do is call another function my_func2()
, which always sets the pointer. GCC
warns me that the pointer might not be set. How can I get rid of the warning?
Here's some simplified code to merely demonstrate it.
int bla;
void my_func2(int *ptr) {
ptr = &bla;
}
void my_func1() {
int *ptr;
//ptr=0;
my_func2(ptr);
}
If the line ptr=0
is uncommented, then the warning goes away. I don't want to set the variable because it does nothing since the my_func2()
sets it.
The gcc
warning message is
warning: 'ptr' is used uninitialized in this function [-Wuninitialized]
Any help would be greatly appreciated.
my_func2
doesn't do anything (observable). Your compiler is right, ptr is unitialized.my_func1
doesn't change at all and stays uninitialized.ptr
is never set. The variableptr
insidefunc2
is local. If you want to set the one in main, you'll have to pass func2` anint **
and set that.