0

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.

5
  • my_func2 doesn't do anything (observable). Your compiler is right, ptr is unitialized.
    – tkausl
    Commented Dec 8, 2017 at 18:15
  • It sets the ptr. I said this simplified code. My function does serve a purpose.
    – Paul
    Commented Dec 8, 2017 at 18:17
  • 1
    It doesn't. It sets its local copy, and then does nothing with it, the local variable in my_func1 doesn't change at all and stays uninitialized.
    – tkausl
    Commented Dec 8, 2017 at 18:17
  • The compiler is correct, ptr is never set. The variable ptr inside func2 is local. If you want to set the one in main, you'll have to pass func2` an int ** and set that. Commented Dec 8, 2017 at 18:18
  • Okay, how can it set the ptr? Something like this **ptr?
    – Paul
    Commented Dec 8, 2017 at 18:18

1 Answer 1

1

I think what you're trying to do is this:

int bla;
void my_func2(int **pp) {
    *pp = &bla;
}
void my_func1() {
    int *ptr;
    my_func2(&ptr);
    ...
}
1
  • That's it. Thanks everyone for the help. I'll click the checkbox for your answer when I get back. It says I must wait 6 minutes. Fastest answer ever lol.
    – Paul
    Commented Dec 8, 2017 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.