How to find length of array without getting it from user? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to find length of array without getting it from user?

https://code.sololearn.com/cr7c1LvUmNni/?ref=app Here I tried len=sizeof(a)/sizeof(*a) to get the length. But everytime I try to do this, sizeof(a) just takes the total size of the declared array, eg a[30] so sizeof(a) is becoming 30*4=120 So I tried declaring an empty array but it says it is invalid. How can I solve this?

16th Jul 2021, 12:05 PM
Srinath
3 Answers
+ 1
Jazz wow that was a very detailed explaination. Thanks a ton! As you explained VLA, are they similar to dynamic memory allocation or are they completely different? Coz I never heard about it before.
16th Jul 2021, 3:18 PM
Srinath
+ 1
Jazz then aren't all arrays VLAs? I mean reading the name, I thought their size are modified at runtime, like if I declare a[20] then increasing its size, say a[25], are variable length arrays. Isn't this the usual declaration, n for size of array and int arr[n]; What makes it variable, just curious 🤔
17th Jul 2021, 4:03 PM
Srinath
+ 1
Jazz Thanks a ton, this was an absolutely crystal clear explanation. And No,no I knew before array sizes cannot be resized at compile and runtime (anyway I really didn't understand the difference between runtime and compile time before, but I learnt about it only after reading your explanation😅), I just thought, reading their name "variable length arrays", maybe there was a way to change them and that's why I asked. You really made things a lot clearer anyway. Thanks and Cheers!
19th Jul 2021, 4:33 PM
Srinath