I have to find the exact number of elements in an int
array leaving the '\0'
s which are effectuated during declaration. I know that the value '\0'
is equivalent to 0
.i.e ('\0' == 0) = true
.
#include<iostream.h>
int getsize(int arr[])
{
int i = 0,p=1;// initialized to 1 as, if left un-initialised it suffers from undefined behaviour
while(p!=NULL)
{
i++;p=arrr[i]'
}
return i;
}
The code works fine when it is called in the main as:
void main()
{
int testarr[10]={0,5};
cout<<"\nThe size is : "<<getsize(testarr);
}
But when the testarr[]
is modified to int testarr[10]={5,0}
The output becomes
The size is : 1
The problem is that the pre-historic turbo c++ compiler,which I'm unfortunately restricted to reads int
0
and '\0'
the same.I have used NULL
instead but, the code doesn't seem to work. Is there something I'm missing?