I wanted to know why has a static variable been used in this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I wanted to know why has a static variable been used in this code?

Line 17 u see static int num[5]The function is used only once and we dont need the value inside of it to stay after we get out of that. But if we turn it into a normall int it gives us an error. So id be glad if anyone could help me out https://code.sololearn.com/cafF9415Lxw2/?ref=app

30th Oct 2019, 8:44 AM
Emad
2 Answers
+ 3
If you declare num as : int num[5] Then num is a local variable that is destroyed at the end of the function. When the function returns the pointer on num, then num has been destroyed and you should not access this memory area. Declaring it as static, num isn't destroyed at the end of the function! (because it is a static variable, not local variable). Thus, there is no problem returning a pointer to that memory area, because it still exists. For a better understanding, learn a bit of assembly.
30th Oct 2019, 9:07 AM
Théophile
Théophile - avatar
+ 3
I think i totally got it 🤓tnx alllot 🤗🤗
30th Oct 2019, 9:13 AM
Emad