Normally using the same identifier like name of a variable for something like another variable within the same scope generates error by compiler, Is there any technique to actually indicate to compiler that in this scope up to this specific point this name has its own purpose and is used to refer to this variable but after this point the same name is going to refer to something else like another variable with another purpose?
Normally using the same identifier like name of a variable for something like another
Share
If you mean variables, no, there’s not. When you create a variable, it’s tied to a specific type and a specific location. Having said that, there’s nothing stopping you from re-using the same variable for two different things:
You can use a pointer so that it can be changed to point to a different variable but that’s not what you’re asking, I suspect. In any case, unless you use a void pointer and cast it, it’s still tied to a specific type:
If what you’re proposing is something like:
I’m not sure I see the point. I think you could do that by putting the definitions within a new scope (i.e., braces):
but it’s not as if we have a world-wide shortage of variable names. And, if you’re naming your variables well, it’s pretty unlikely that a string and a float would even have the same name (double and float maybe but it’s still an dubious function to add to the language).