+ 3

Global variables in C++

Can I create a global variable inside main() or other function?

3rd Jul 2020, 12:22 PM
Maksim
Maksim - avatar
9 Answers
+ 3
ŠœŠ°ŠŗсŠøŠ¼ ŠšŠ°Š·Š°Š“Š°ŠµŠ², here I wrote a simple program to show how to declare a global variable and use it to handle a uni-dimensional array, which size is unknown at compile-time but is user-defined at run-time. Which means its length is dynamical. In this program, we use the power of dynamic memory allocation. The source code can have multiple variations. You may play around with it and try to use even a more complex logic. šŸ˜‰ On PC, in Visual Studio, for example, the output is easier to see than here in Code Playground. https://code.sololearn.com/cqXZIM1V6oMg/?ref=app
3rd Jul 2020, 4:18 PM
Vasile Eftodii
Vasile Eftodii - avatar
+ 7
Yes you can , but that will no longer be a global variable. It will be known as local variable and will be destroyed as soon as it goes out of scope.
3rd Jul 2020, 12:26 PM
Arsenic
Arsenic - avatar
+ 5
ŠœŠ°ŠŗсŠøŠ¼ ŠšŠ°Š·Š°Š“Š°ŠµŠ² that is the problem with arrays, its size should be known before hand. you can use dynamic memory alocation to create an array of required size at runtime. But this might lead to memory leak. But there is a safe alternative to this in C++ ,which is famously known as *vector*
3rd Jul 2020, 12:35 PM
Arsenic
Arsenic - avatar
+ 4
A variable which can be accessed by all the available functions is known as global variable . If u declare a variable inside the main() than its scope will be inside main() only u can't access it in other functions so that will be local only . Global variable should be declared outside of all the function than only it will be known as global. To use a variable as global which is declared inside main () we can use the concept of pointer . We can use the variable of main() by its reference.
4th Jul 2020, 5:00 PM
DevPS
DevPS - avatar
+ 2
I need to declare an array of size that user will input and to use this array in functions without passing it into arguments. How can I create this dinamic array (I don't know the size before main() function)?
3rd Jul 2020, 12:28 PM
Maksim
Maksim - avatar
+ 2
You can create a global array by declaring array before void main() or int main() Like this //Header files int arr[5]; //Main This array can be used by any method/functions without passing as arguement which is what you want to do. And as you said size is not defined i.e dynamic array, you can use new operator to declare an array of size given by user.
3rd Jul 2020, 12:40 PM
Alaska
Alaska - avatar
+ 2
In case, if sololearn doesn't have lesson for new operator, here is the link you can refer. https://www.geeksforgeeks.org/new-and-delete-operators-in-cpp-for-dynamic-memory/ Happy codingšŸ˜Š
3rd Jul 2020, 12:41 PM
Alaska
Alaska - avatar
+ 1
Thank you for your support! I am realy inspired by these examples.
3rd Jul 2020, 4:54 PM
Maksim
Maksim - avatar
+ 1
No, if you create any variable inside method then it is no longer global that will be local variable, which can be accessed only inside the methods. Global variable should be outside the method and inside the class.
3rd Jul 2020, 6:08 PM
Divyashree K.N
Divyashree K.N - avatar