简体   繁体   中英

How to find the number of user-defined elements in an int array?

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 .ie ('\\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?

How to find the number of user-defined elements in an int array?

There is no way to distinguish a zero that was specified by the programmer, from a zero that was generated by the compiler.

The problem is that the pre-historic turbo c++ compiler,which I'm unfortunately restricted to reads int 0 and '\\0' the same.

That behaviour is not specific to your ancient compiler. It is true for all C++ compilers. The value of 0 is exactly the same as '\\0' . However, there is no '\\0' involved in your program.


 while(p!=NULL) 

Don't compare an integer with NULL . That is going to confuse anyone reading your code (which since C++11 may be ill-formed depending on how the standard library has chosen to define NULL ). Since it is equivalent to comparing with zero, just use while(p) .


What your function does, is it counts the number of non-zero elements in the array and until a zero is reached, except the first value is ignored and always counted as 1. If the array does not end in a zero (in other position than first), then the array is accessed over bounds, and there will be UB.

Essentially, it works just like strlen , but for integers, and the first element is treated differently.

So the expected output would be:

{0,0,1,2} -> 1
{0,1,0,0} -> 2
{1,0,0,0} -> 1
{0,0,0,0} -> 1
{0,1,2,0} -> 2
{0,1,2,3} -> Any or no output is expected, because UB

can't I check for null pointers

No. Your array does not contain pointers. It contains integers.

testrarr[10]={5,0} as, by default ,during initialization it fills all the values in the array as \\0 aka null character not nullpionter –

No. The array contains integers, so it is not filled with null character. Zero initialization fills the array with zero integers (which has the same value of null character, so the distinction is subtle).

C misuses the term "array" slightly. An array is an organisation of data, a buffer is a place to put data. So when we declare a buffer in C

int testarr[10];

we are declaring a buffer which can hold ten integers, not an array of ten integers. In fact the values may be "illegal integer" trap representations.

When you do

int testarr[10] = {0,5};

you've got a buffer of ten integers, and array of only two. But, partly because the array / buffer confusion is ingrained, C won't tell you. It's normally necessary to keep a separate value, N.

int testarr[10] = {0, 5};
int N = 2;

Now when we pass the array to a subroutine, we pass the address, plus N.

 int sum(int *x, int N)
 {
    int answer = 0;
    int i = 0;

    for(i=0;i<N;i++)
      answer += x[i];

    return answer;
 }

 int testarr[10] = {0, 5}:
 int N = 2;

 total = sum(testarr, N);

Note that we can also call sum on array slices, or on dynamically allocated arrays. The function is completely unaware of any unused integer slots after the data, or if data is static, on the stack, or on the heap.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM