+ 1
Can I find size of array in C language
Do C language have dynamic array and can I find length of array.
9 Antworten
+ 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;
}
+ 4
For charecter array, you can use
length = strlen(array);
For others,
length=sizeof(array) /sizeof(array[0]) ;
+ 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.
0
You can use i. sizeof(array)
or. ii. strlen(array)
I hope, you will understand.
Thank you
- 1
Here's the code just check out Muhammad Hasnain Raza
- 2
Muhammad Hasnain Raza yes of course