0

I write a function in c++ that should count number of elements an array has. function receive array as its parameter. so I try the following method:

int countArray(int a[])
{
    int size = 0;
    while(a[size] != NULL)
    {
        size++;
    }
    cout<<"number of array elements are : "<<size<<endl;
}

this function work but not perfectly. when i pass an array to this function which has same number of elements as its size int one[3] = {1,2,3} or an unsized array it will return result with one more element. for example for the previous array one[3] it will display number of array elements are 4.
but in other situation it work fine. for example if I pass an array that has less element than its size int two[4] = {1,2,3} it will work.
I should use array in this example not vector or struct , so what should i do or what is the reason that function doesn't work with that kind of array as its parameters.

9
  • 5
    You cannot find that size. You need to pass it as an argument, use a template to pass the size or use a proper c++ container like std::vector. The idiomatic way of working on containers in modern c++ is to pass ranges (pairs of iterators). Commented Jul 11, 2018 at 14:19
  • 3
    a[size] != NULL does not check whether there's an element in the array at that index Commented Jul 11, 2018 at 14:20
  • 1
    Possible duplicate of How to get size of dynamic array in C++ Commented Jul 11, 2018 at 14:21
  • 1
    For sanity's sake replace NULL with nullptr. If it does not compile then your are not doing it correctly. Commented Jul 11, 2018 at 14:22
  • 3
    @AliDK Then use std::vector. Variable length arrays are not allowed in portable C++ either. Commented Jul 11, 2018 at 14:23

4 Answers 4

6

Once an array have decayed to a pointer (to its first element), there's no way of getting its size.

The loop you have can (and most likely will) go out of bounds and you will have undefined behavior.

There are three possible solutions:

  1. Use std::array instead

  2. Use std::vector instead

  3. Use array-size deduction with templates:

    template<size_t N>
    int countArray(int (&a)[N]) { ... }
    

Also note that C++ doesn't have the concept of "null" values. The symbolic constant NULL is for pointers only.

Sign up to request clarification or add additional context in comments.

1 Comment

as I mention I try std::array but it is for fixed size array and array parameter should be unsized. can I use std::vector as unsized array in function parameter? can you help with an example that how should I do this.
1

This function doesn't work when an array is used as a parameter, it decays into a pointer, and NULL is 0:

int a[5] = { 1,2,0,0,0 };
int countArray(int a[])
{
    int size = 0;
    while (a[size] != NULL)
    {
        size++;
    }
    cout << "number of array elements are : " << size << endl;
    return size;
}

The right answer is 5, but the output is 2.

You should change this function as follows:

int countArray(int a[], int size)
{
    cout << "number of array elements are : " << size << endl;
    return size;
}

And then you can call it like this:

int a[5] = { 1,2,0,0,0 };
countArray(a, 5);

1 Comment

thank you for mentioning this point that I didn't realize before. i think I have to change the method i use because its not prefect.
0

I should use array in this example not vector or struct

From reading your comments and your inital post i just try to guess what data you are given and what you have to do. can it be that you are given an integer array with the following properties:

  1. at least one element
  2. the last element is guaranteed to be 0
  3. no other element except the last is 0

Then - and only then - the solution you tried would work. The example with int two[4] = {1,2,3} works because you create an array with 4 elements. but only initialize 3 with aggregate initialization. so the remaining (and so the last element) is set to 0.

The check for NULL with integers works the same as a check with 0.

2 Comments

it is not necessary that array should has at least one element, i suppose my function work if someone pass an empty array to it and it should return zero as its number of elements. and I got your point that why function work with larger array with less elements, because remaining elements are zero which is equal to null
the problem is: if the array is empty you are not allowed to access the first element. and thats what you do unconditionally in the while loop. and again: there is NO way you can "count" the array size, except you know which value the last element has (such as a delimiter)
-1

Switch to java, javascript, or c#, or use the std::vector instead of arrays.

C++ is old and they didn't think of array size as important in the core because they figured you would be using classes that would free the world from pointer arithmetic and type size calculations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.