Why is the last output not 0? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 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

30th May 2019, 10:38 PM
Spandan Bhattacharya
Spandan Bhattacharya - avatar
14 Réponses
+ 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 };
30th May 2019, 11:18 PM
Cluck'n'Coder
Cluck'n'Coder - avatar
+ 4
Ha, interesting. Haven't tried to do it (initialize a flexible one), didn't know it doesn't work.
30th May 2019, 11:22 PM
HonFu
HonFu - avatar
+ 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.
30th May 2019, 10:58 PM
Bilbo Baggins
Bilbo Baggins - avatar
+ 3
You are right! Go with "memset(arr,0,a*sizeof(int));"
30th May 2019, 11:17 PM
Bilbo Baggins
Bilbo Baggins - avatar
+ 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.
30th May 2019, 10:57 PM
HonFu
HonFu - avatar
+ 2
Ok thanks,I will then give it a initialization.
30th May 2019, 11:02 PM
Spandan Bhattacharya
Spandan Bhattacharya - avatar
+ 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.
30th May 2019, 11:07 PM
Cluck'n'Coder
Cluck'n'Coder - avatar
+ 2
i did it but still it is not working,(hopefully i did it as instructed)
30th May 2019, 11:12 PM
Spandan Bhattacharya
Spandan Bhattacharya - avatar
+ 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.
30th May 2019, 11:42 PM
Bilbo Baggins
Bilbo Baggins - avatar
+ 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.
31st May 2019, 1:14 AM
Cluck'n'Coder
Cluck'n'Coder - avatar
+ 1
The declaration "int arr[a];" disappeared from your code, and the compiler complains because he finds an array not declared!
31st May 2019, 9:27 AM
Bilbo Baggins
Bilbo Baggins - avatar
+ 1
finally it works now!! Thanks everyone for clearing my doubt!
31st May 2019, 9:30 AM
Spandan Bhattacharya
Spandan Bhattacharya - avatar
0
Ok i used "memset(arr,0,a*sizeof(int));" but still its not working.(again hopefully i used it correctly)
31st May 2019, 9:18 AM
Spandan Bhattacharya
Spandan Bhattacharya - avatar
0
ok so i have to atfirst use"int arr[a];" then use"memset(arr,0,a*sizeof(int));"
31st May 2019, 9:29 AM
Spandan Bhattacharya
Spandan Bhattacharya - avatar