[Solved] Array to pointer conversion. How to find the size of array in C++? [duplicate]


There is none. Once the array-to-pointer conversion happens, the size information is gone. If you need it, you have to pass the size as a separate argument.

Since pArr is a pointer, sizeof(pArr) gives the size of the pointer. Your pointers seem to be double the size of ints; that is the case on x86-64, for example.

One option to preserve the size would be to turn the processing function into a function template parameterised on the array size:

template <std::size_t N>
void printArray(int (&arr)[N])
{
  for (std::size_t i = 0; i < N; ++i) {
    std::cout << arr[i];
  }
}

Note that this means you’ll normally need to implement the template in a header file, and you’ll get one instantiation for each array size with which you call it. And it will only be callable with actual C-style arrays.

2

solved Array to pointer conversion. How to find the size of array in C++? [duplicate]