Paul
Paul

Reputation: 113

c, get rid of uninitialized warning error

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.

Upvotes: 0

Views: 303

Answers (1)

Lee Daniel Crocker
Lee Daniel Crocker

Reputation: 13171

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);
    ...
}

Upvotes: 1

Related Questions