'How do I measure the length of an int array in C++?

My goal is to print all elements of an array of integers regardless of its length. I would like to print it in Python list format, but then I got this error. Here is my code

int measure(int n[])
{
    int num=0;
    while (n[num]) { num++; }
    return num;
}

void show(int n[])
{
    int a = measure(n);
    for (int i=0; i<a; i++) {
        if (i==0) { printf("[%d,",n[i]); }
        else if (i==a-1) { printf(" %d]",n[i]); }
        else { printf(" %d,",n[i]); }
    }
}

int main(void)
{
    int arr[10] = {1,2,3,4,5,6,7,8,9,10};
    show(arr);
    
}

It is supposed to print this: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] but I got this instead: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1935094528, -1664206169]

then I replace show() with this:

int i=0;
    while (n[i]) {
        if (i==0) { printf("[%d,",n[i]); i++; }
        else if (n[i+1] == NULL) { printf(" %d]",n[i]); break; }
        else { printf(" %d,",n[i]); i++; }
    }

and then I got these:

main.cpp:23:28: warning: NULL used in arithmetic [-Wpointer-arith]
   23 |         else if (n[i+1] == NULL) { printf(" %d]",n[i]); break; }
      |                            ^~~~
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -680101376, -1228044632]

Why does this happen?

c++


Solution 1:[1]

How do I measure the length of an int array in C++?

You can use std::size to get the size of an array with known size:

int arr[10] = {1,2,3,4,5,6,7,8,9,10};
std::cout << std::size(arr);

Why does this happen?

Because the continue condition for your loop is "while the element is not 0". There are no elements with the value 0, so the loop doesn't end before exceeding the end of the array, at which point your overflow the array and the behaviour of the program becomes undefined.

The parameter n of the functions measure and show is not an array of known size. Since n isn't an array of known size, you cannot use std::size to get its size. In fact, although it looks like an array of unspecified size, n is adjusted to be a pointer to element of such array. There is no general way to measure the size of an array given a pointer to its element.

In cases where you want to pass array of any size into a function, and also need to know the size of the array within the function, a good solution is to use a span parameter:

void show(std::span<int> n)

Solution 2:[2]

There simply is no "measuring" the length of arrays in C++. You have to know up front, and pass that info to any functions or methods you pass the array to. You can also use a specific "tag" value (as you have implicitly done here) but if you do that, you have to set the tag yourself, and that means you have to know the array length to set the tag. And you must be very sure that the tag does not equal any valid data value in your array.

A different approach would be to use std::vector instead of an array. That data type gives you all the functionality of an array but also provides variable length arrays and the ability to inquire the current length.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2