Reputation:
I have a switch statement like this
switch(option)
{
case 1:
{
int randomVariable;
}
case 2:
{
}
case 3:
{
\\ I want to use randomVariable here but it is not letting me since it is not in the same scope
}
}
}
Any Ideas on how to solve this issue? Please note that randomVariable
must be declared under case 1:
and randomVariable
is an array. Please try to stick to std libraries and please please please do not use vectors as this is for a project and vectors are not at all discussed in this class.
Upvotes: 1
Views: 226
Reputation: 42085
The region in which you are able to access a variable is called its scope. For local non-static variables the boundaries of this scope are defined with curly braces ({
... }
), so:
{
...
int a = 0;
switch (option)
{
case 1:
{
int b;
a = 2;
} // <-- the scope of b ends here
...
}
} // <-- the scope of a ends here
Please note that
randomVariable
must be declared inside ofcase 1:
as this is the option that creates the array
Since you are programming in C++, instead of C-style array use std::vector
, which is the container holding elements within the continuous block of memory just like an array, but it can change in size:
#include <vector>
...
{
std::vector<int> myVector;
switch (option)
{
case 1:
{
int size;
// value retrieved in run-time is assigned into size here
myVector.resize(size, 0);
}
case 2:
{
// you can use your vector here:
if (myVector.size() > 3)
myVector[2] = 7;
}
...
}
} // <-- end of myVector's scope
in this example myVector.resize(size, 0);
resizes the memory internally used by vector so that it's big enough to hold size
elements and if its size has been increased it also inserts new elements to this memory and initializes them to 0
. What is important here is that myVector
is an object with automatic storage duration, which means that when the execution goes out of the scope where it has been defined, the memory is cleaned up automatically. This saves you from the ugly memory management that you would have to take care of in case you use dynamically allocated C-style array.
Upvotes: 3
Reputation: 4748
Edit:
After your comments, for your use case, you probably want something like this:
int * myArr = 0;
int myArrSize = 10;
switch (option) {
case 1:
if (myArr != 0) {
// Clean up memory if we are re-initializing
delete [] myArr;
}
myArr = new int[myArrSize];
break;
case 2:
break;
case 3:
int test = myArr[1];
break;
}
// Clean up memory when we are done with the array
delete [] myArr;
Upvotes: 1
Reputation: 1892
here you go...
Hope this will help..!!!
Upvotes: 0
Reputation: 49473
A variable's scope is only within the block it is defined, so no it is not possible to define the variable in one scope i.e. within case 1:
and access it outside the scope, unless you intend to combine the cases for 1 and 3 together without an explicit block { ... }
.
Define the randomVariable
in a scope visible throughout the switch statement, so that any case can access this variable.
{
int randomVariable;
switch(option)
{
case 1:
{
}
case 2:
{
}
case 3:
{
\\ I want to use randomVariable here but it is not letting me since it is not in the same scope
}
}
}
The scope above the switch could be a function, or another block depending on your code.
Upvotes: 0
Reputation: 70108
With your restriction that it has to be initialized in case 1, then just make the variable a pointer and use variable = new something();
in case 1.
By the way, this scope has nothing to do with "global".
Upvotes: 1