+ 3
Why is the last output not 0?
Here i have tried to use a variable to set the size of an array,By default an array has a value 0,so the last array should have a value 0 but here it shows a very big different number each time.Plz can anyone explain the cause of this situation? https://code.sololearn.com/cy5c4iWy8aR9/?ref=app
14 Antworten
+ 4
@Spandan Bhattacharya
You can't initialize arrays that don't have a constant as size and because arr[] has its size determined by the variable 'a', the { 0 } initializer won't work.
You'd either have to use memset() or declare arr[10] = { 0 };
+ 4
Ha, interesting. Haven't tried to do it (initialize a flexible one), didn't know it doesn't work.
+ 3
"By default an array has a value zero" is not always correct. It is true if the array is static, or global, which means defined outside of all functions; in that case it will go in a memory zeroed at start of the process. It is false if it is defined inside a function; in that case it will go on a different memory, the stack, which is never initialized.
You can initialize it declaring "int arr[n] = {0};" or "memset(arr,0,n*sizeof(int));"
And please note that setting an array size with a variable is not a C feature, but just an extension of GCC. It is not supported for example by the Microsoft compiler.
+ 3
You are right! Go with "memset(arr,0,a*sizeof(int));"
+ 2
You are declaring an array with a given size, but you don't initialize it, so every spot has an arbitrary value.
If you initialize by setting at least one spot to zero (arr[a] = {0};), all the spots will be set to zero.
+ 2
Ok thanks,I will then give it a initialization.
+ 2
Variable length arrays conform to C99. It's just that MSVC is an outdated compiler that only supports C89 features. So for maximum portability you'd likely want to stick to C89 and that means you also can't have mixed declarations, declare integers in for-loops or use designated initializers for structs.
+ 2
i did it but still it is not working,(hopefully i did it as instructed)
+ 2
Cluck'n'Coder
It sounds like VLA were required in C 1999, but became optional in C 2011:
https://softwareengineering.stackexchange.com/questions/314838/why-were-variable-length-arrays-made-optional-in-c-2011
Anyone knows if they have been implemented in MSVC 2019?
Regarding integer declaration inside for loops and declarations in the middle of the scope, both are supported from MSVC 2017.
+ 2
@Bilbo Baggins
Interesting, I wasn't aware VLAs were made optional with the C11 standard. Having them be deprecated and fall by the wayside like gets() would seem like a far better choice.
Not sure about MSVC 2019, but chances are if it's a feature supported by C++ they likely support it in MSVC (don't quote me on that). Last time I checked they still had the non-standard _snprintf() alternative so the chances are slim. If you find out, I'd be interested to know.
+ 1
The declaration "int arr[a];" disappeared from your code, and the compiler complains because he finds an array not declared!
+ 1
finally it works now!!
Thanks everyone for clearing my doubt!
0
Ok i used "memset(arr,0,a*sizeof(int));"
but still its not working.(again hopefully i used it correctly)
0
ok so i have to atfirst use"int arr[a];"
then use"memset(arr,0,a*sizeof(int));"