tags:

views:

74

answers:

4

Hello,

I was just doing my first app in C and i have this warning (edited) : unused variable pp

int compteur =  1;
int *p = &compteur;
int **pp = &p;

I was just trying to make pp pointing on the adress of the variable p

Forgive me if that's a stupid question, but in my book they don't talk about pointers on pointer. Thanks

+3  A: 

That's perfectly legitimate. Your compiler is just informing you that you made a variable, then didn't do anything with it - normally indicating a programming error.

DeadMG
+3  A: 

I can suppose, that that's not error, but warning. "unused variable pp" means, that you just don't use variable pp in further code.

Kel
oh right, so there is no need of worrying :)
Tristan
+2  A: 

Well if that's the only thing your program does, then yes, the compiler is correct to warn you about unused variables, because you're not doing anything with them!

If you were to do e.g. printf("%d\n", **p);, then the warning should go away.

Oli Charlesworth
+1  A: 

i have this warning (edited) : unused variable pp

Kudos for respecting (and worrying and asking) the compiler warning message. Keep that up. (I always compile with -Werror so that "warnings" are treated as "errors." I always find it helpful.)

Quoting from the gcc warnings page:

-Wunused-variable

Warn whenever a local variable or non-constant static variable is unused aside from its declaration. This warning is enabled by -Wall.

Strictly speaking, it does make sense to declare a variable and then not use it. That's the case for pp and compiler is alerting that. In this particular case,

  • either you use pp in some required way, or

  • use it in the "no-op" way.

Following is the code for "no-op" way:

int main() {

    int compteur = 1;
    int *p = &compteur;
    int **pp = &p;
    (void) pp;            // no-op use of pp
}

For further details on no-op use, see the other thread: http://stackoverflow.com/questions/3986295/what-does-the-following-code-do

ArunSaha