Can I find size of array in C language | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can I find size of array in C language

Do C language have dynamic array and can I find length of array.

12th Jul 2020, 8:48 AM
Muhammad Hasnain Raza
Muhammad Hasnain Raza - avatar
7 Answers
+ 1
Akash Kumar yes. But append '\0' is not always need. If need, then you should append before calculating length. And question is about just finding length.. There may be a lot of ways. If those all, need to specify special cases, 1024 charecter not enough... We can specify Capacity at declaration. So we can know that at begin. If we don't specify then length = capacity, that we calculate by those methods.. An example: #include <stdio.h> #include<string.h> int main() { int a[]={1,2,3,4,5},n; char s[]="strings"; n=sizeof(a)/sizeof(a[0]); printf("size of array elements=%d\n",n); printf("%ld",strlen(s)); return 0; }
13th Jul 2020, 8:53 AM
Jayakrishna 🇮🇳
+ 4
For charecter array, you can use length = strlen(array); For others, length=sizeof(array) /sizeof(array[0]) ;
12th Jul 2020, 9:51 AM
Jayakrishna 🇮🇳
+ 1
yes using strlen returns you the length of a char array, but it's time complexity is O(n) everytime you call it. and it does not work for int array since it was meant for strings. best way that professionals use is to instead use a variable to keep track of the size of the array.
13th Jul 2020, 5:08 AM
Shen Bapiro
Shen Bapiro - avatar
0
You can use i. sizeof(array) or. ii. strlen(array) I hope, you will understand. Thank you
14th Jul 2020, 7:50 AM
Subhajit Dey
Subhajit Dey - avatar
13th Jul 2020, 7:37 AM
Navneet Kaur💫
Navneet Kaur💫 - avatar
- 1
Here's the code just check out Muhammad Hasnain Raza
13th Jul 2020, 7:38 AM
Navneet Kaur💫
Navneet Kaur💫 - avatar
- 2
Muhammad Hasnain Raza yes of course
13th Jul 2020, 6:59 AM
Navneet Kaur💫
Navneet Kaur💫 - avatar