{"id":706,"date":"2022-08-30T15:05:49","date_gmt":"2022-08-30T15:05:49","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/while-variable-is-undefined-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:05:49","modified_gmt":"2022-08-30T15:05:49","slug":"while-variable-is-undefined-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/while-variable-is-undefined-collection-of-common-programming-errors\/","title":{"rendered":"While variable is undefined&#8230;?-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li>Declare the variable &#8220;static&#8221; 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&#8217;ve just done, or it may get a random first value. You then need no testing at all.\n<p>Hope that helps.<\/p>\n<\/li>\n<li>You can check if variable is null. However, you have to declare the variable before using it. And you can&#8217;t declare it multiple times in the same function. Also, how are you passing the variable to the function? If you don&#8217;t want tit o be reset, then consider passing the variable as a parameter by value.\n<p>Also, the while, as you put it makes no sense. An if would be much better suited&#8230; either that or you need to post the exact code.<\/p>\n<\/li>\n<\/ul>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:42:20. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Declare the variable &#8220;static&#8221; 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&#8217;ve just done, or it may get a random first value. You then need no testing at [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-706","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/706","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=706"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/706\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}