Set Variable?


i have a strange (at least for me :wink: ) problem here:

this is a part of the code of a function:

  if (find_text("testp", SMS) > 0)
    static char* SMS = "function";

  return SMS;

i then serial.println the return (SMS) wich should be "function" since it gets cought by the if statement. (i know this, because it also prints "DEBUG" in the serial monitor. The thing is, that it does NOT set SMS to "function". Anyway, when i copy paste the line static char* SMS = "function"; directly before "return SMS;" it works! and i get "DEBUG" AND "function" in the serial monitor!

Why can i not set the variable SMS within the if statement?

Thank you so much,


You have at least two variables named SMS (or your snippet would not compile).

The static pointer is only visible between the curly brackets that enclose its definition.
It will be set once to point to "function", independent of the if.

The returned value SMS is the value of the other variable named SMS.

For more details, see Nick Gammon's Useful Links at the top of the Forum, then look for C++ Programming --> Scope.