G0867532
G0867532

Reputation: 82

Referencing identifiers in code but not using them

Ricently i was writing the code, and make a mistake like that:

int f () 
{
 . . .
}

int g;

int main()
{
 . . .
 if (f) { 
  // this is actually true
 }
}

Later, i've check and constructions like:

f;
g;

Does not give even any compiller errors. It is seems kind of logical, But what is the purpose of this? How it can be used? To check if some identifier is present? To force compiler not to optimize function or variable?

UPD: Just checked and behavior is the same using VS and minGW.

Upvotes: 0

Views: 56

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409482

Functions decay to pointers to themselves, i.e. plain f is equal to &f. And when using a pointer in a condition like that, then it checks if the pointer is null or not.

In other words,

if (f) { ... }

is equal to

if (&f != NULL) { ... }

It might not make much sense in this case (as &f will always be non-null) but pointers to function are quite usable in other places.

Upvotes: 4

Related Questions