While variable is undefined…?-Collection of common programming errors

  • Declare the variable “static” and there will be only one copy of it for all your calls. In other words: int MyFunction( void ) { static int my_static_variable = 0; } Be sure to initialize it the way I’ve just done, or it may get a random first value. You then need no testing at all.

    Hope that helps.

  • You can check if variable is null. However, you have to declare the variable before using it. And you can’t declare it multiple times in the same function. Also, how are you passing the variable to the function? If you don’t want tit o be reset, then consider passing the variable as a parameter by value.

    Also, the while, as you put it makes no sense. An if would be much better suited… either that or you need to post the exact code.

Originally posted 2013-11-09 21:42:20.