Determine the number of rows in an array

Hi,

I have a dll with a function that takes an array as an argument like this

 
  void Process(const double price[])


What is the best way to determine how many rows are in the array?

Thanks,

John
Last edited on
I'm pretty sure you don't need a number inside the "[]" when you're using an array as a parameter/argument. It'll know how many rows/columns are in the array when it's passed in.

If you need to know for a different reason, use this method if really needed:

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

	int count = 0;

	for (int elem : arr)
	{
		std::cout << count++ << ' ';
	}
}


There are other ways, but this is the most convenient for me.
What is the best way to determine how many rows are in the array?

Hopefully this is a DLL you can manage and modify the code to be less of a PITA badly written.

The function will work if:

1. the number of array elements are hard-coded into the function's code. Pass an array of different size and things will go bad, real bad, depending on if the array size is smaller or larger than expected.

2. you pass a pointer to the last element of the array.

3. the array contains a sentinel value at the end (the reason why a C string char array has '\0' as the last value.)

When using a regular array you really should also pass the size (number of rows/elements) into the function.

void Process(const double price[], int size)

Using a vector making passing one into a function much easier.

void Process(const std::vector<double>& price)

A vector keeps track of the number of elements it contains and can be accessed at any time with the size() method.

When using a multi-dimensional container vectors really shine. If you have a 2D vector:

void printMatrix(std::vector<std::vector<int>>& matrix)

and calling the function:

printMatrix(myMatrix); // function call

The C++ STL containers are designed to make a lot of the tedious book-keeping of regular containers less of a hassle.

zapshe wrote:
It'll know how many rows/columns are in the array when it's passed in.
No, it won't. A regular array has no inherent way of knowing what its size is, whether passed into a function or not. std::vector was coded to overcome this problem.
Last edited on
No, it won't. A regular array has no inherent way of knowing what its size is, whether passed into a function or not. std::vector was coded to overcome this problem.

My bad, wrong wording. Since the array will be passed in, you don't need to specify a size or anything. So you don't need a number within the "[]", not that it knows what it's size is but that you'll be working with the array you've already declared a size for.
One problem is any reference to an array's size is external to the array. std::vector keeps track of its size.

Define an array in main(), without a global variable for the size, a local const defined in main() should be preferred over a global, there is no way a function can easily know what the array's size is. Unless you also pass the size into the function or use a sentinel ending value. As C string char arrays work.
Last edited on
There's also "std::size()" which you can use to find the size of the array, like this:

1
2
3
4
5
6
7
int main()
{
	int arr[50];
	std::cout << std::size(arr);

	return 0;
}

Output:
50


But it sadly wont work within a function where the array will decay. But you can use that number to pass into your function (along with several other ways you can do it).
Last edited on
Hello skiner36,

void Process(const double price[])

What is the best way to determine how many rows are in the array?


This example is a 1D array it has no rows only columns. At least that is my understanding.

As zapshe has mentioned when a one or 2D array is passed to a function it will degrade to a pointer. Not that you really notice it.

If you set up the code properly something like:
1
2
3
4
5
6
7
8
9
10
int main()
{
    constexpr size_t MAXSIZE{ 10 };

    double price[MAXSIZE];

    Process(const double price[], MAXSIZE);

    return 0;
}

That is as long as the function takes two parameters. If not then either the function in the dll will need to be changes or the function will have to determin how big the array is.

That is my 2 cents. If anyone sees anything wrong with it let me know.

Hope that helps,

Andy
personally, to me, a constant is not a variable. (variables... vary). I am fine with global constants where appropriate, though a namespace or enum for them is critical for larger programs.
Here, I would rank the solutions as ..
1) vector .. its the right way to do it
2) array + size passed into function. Allows function to work on different sized arrays in same code.
3) global constant size. Lots of reasons this isnt ideal. But it will work, and again, I personally do not get the jitters over a constant that I would over a global variable. Its not like you can accidentally change the value of the constant as a side effect like you can a variable...
Last edited on
Topic archived. No new replies allowed.