Thread: An equivalent "strlen function" for integer arrays?

1. An equivalent "strlen function" for integer arrays?

Good day C gurus

I was trying to code the biggest commum divisor for two number program but was confronted to a problem

here is my code by the way

Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
int a=100;
int b=34;
int diva[100]={0};// is the array in which i store the divisors or the numbuer a;
int divb[100]={0};// is the array in which i store the divisors or the numbuer b;
int max=1;
int counta=0;int countb=0 ;int i; int j=0;int k=0;
for(i=1;i<a;++i)
{
if(a%(a-i)==0)
{
diva[j]=a-i;
j++;
}

}

for(i=1;i<b;++i)
{
if(b%(b-i)==0)
{
divb[k]=b-i;
k++;
}

}
for(j=0;diva[j]!=0;++j)
{
printf(" the divisors of the number %i are %i\t",a,diva[j]);
++counta;  // I needed to use a counter to know how many elements in my array diva I do have
}

for(k=0;divb[k]!=0;++k)
{
printf("\nthe divisors of the number %i are %i\t",b,divb[k]);
++countb; // I needed to use a counter to know how many elements in my array diva I do have
}

for(j=0;j<=counta;++j)
{
for(k=0;k<=countb;++k)
{
if(diva[j]==divb[k] && diva[j]>max )
max=diva[j];
}

}

printf(" the biggest commun divisor  is:%d", max);
}```
My code is fine, but during the coding process i got stuck with the following problems and i'm just trying to understand them:

• If i don't initialize my diva and divb arrays to 0 I keep getting strange numbers! why?
• Is there any way for int array to ask the progrem to end the loop when i<strlen(array) in case of integer array or like arra[i]!='\0"?

As you can see, im using the counters (counta and countb) to figure out the necessary loops numbers.

2. You already have a count of the number of elements in diva, it's j.

> for(j=0;diva[j]!=0;++j)
So if you use another var, you can simply write

for ( x = 0 ; x < j ; x++ )

> If i don't initialize my diva and divb arrays to 0 I keep getting strange numbers! why?
Because they're filled with garbage values, yet you try to use diva[j]!=0 test to find the end of the valid data.

> Is there any way for int array to ask the progrem to end the loop when i<strlen(array) in case of integer array or like arra[i]!='\0"?
There is no standard nul integer in the same way that there is a nul character. strlen works on char arrays, because everyone agrees that \0 marks the end of strings, so it's useful to provide a function.

3. > If i don't initialize my diva and divb arrays to 0 I keep getting strange numbers! why?

Because that is just the way C is. Local variables are uninitialized by default1.

> Is there any way for int array to ask the progrem to end the loop when i<strlen(array) in case of integer array or like arra[i]!='\0"?

Yes, but you would have to code this yourself. You would need to pick a sentinel value, a special integer value you would never need (for char strings, this is '\0'). Then you could write an intlen() function that works like strlen for int arrays, and checks for that special value. However, you would have to remember to always terminate your int arrays with this value every time you add a new element, or intlen() would not work correctly (just like forgetting to null terminate your string).

As a side note, '\0' is a character literal (like 'a' or '!'), and as such has a numeric value which is zero. Thus, you could use it as your sentinel value if you never need to keep the actual value 0 in diva or divb.

But I still prefer the counta/countb method you are currently using. It's the simplest/cleanest IMO.

1 Technically this only applies to automatic storage duration local variables (non-static), but that's the default and by far the most common case, so no need to worry about static local variables just yet.

4. Thanks a million to both of you Salem and Anduril!!

Each time i come here I got valuable informations!!