0

I usually use vectors in C++, but in a particular case I have to use arrays which I'm not used to. If I do this:

// GetArraySize.cpp

#include <iostream>

#include <conio.h>   // remove this line if not using Windows

int main(void)
{
  int myArray[] = { 53, 87, 34, 83, 95, 28, 46 };

  auto arraySize = std::end(myArray) - std::begin(myArray);

  std::cout << "arraySize = " << arraySize << "\n\n";

  _getch();    // remove this line if not using Windows

  return(0);
}

This works as expected (arraySize prints out as 7). But if I do this:

// GetArraySizeWithFunc.cpp

#include <iostream>

#include <conio.h>    // remove this line if not using Windows

// function prototypes
int getArraySize(int intArray[]);

int main(void)
{
  int myArray[] = { 53, 87, 34, 83, 95, 28, 46 };

  int arraySize = getArraySize(myArray);

  std::cout << "arraySize = " << arraySize << "\n\n";

  _getch();    // remove this line if not using Windows

  return(0);
}

int getArraySize(int intArray[])
{
  auto arraySize = std::end(intArray) - std::begin(intArray);

  return((int)arraySize);
}

On the line auto arraySize = std::end(intArray) - std::begin(intArray); I get the error:

no instance of overloaded function "std::end" matches the argument list, argument types are: (int *)

What am I doing wrong?

I should mention a few things:

-I'm aware that with C++ 17 I could use std::size(myArray), but in the context I'm working in I can't use C++ 17

-There may be other / better ways to write a getArraySize() function, but moreover I'm trying to better understand how old-style arrays are passed into / out of functions

6
  • "What am I doing wrong?" Well, int main(void) should be int main(). And return(0); should be return 0;. And return((int)arraySize); should be return (int)arraySize;. Instead of a C array, you should use std::array. If you insist on passing in a C array, you should also pass in its length (if its length is something you care about). So int getArraySize(int arraySize) { return arraySize; }, which is a bit redundant. Commented Feb 9, 2019 at 13:22
  • 1
    Don't do an array to pointer decay clang.llvm.org/extra/clang-tidy/checks/…. Commented Feb 9, 2019 at 13:30
  • 1
    If this is C++, main(void) should be replaced with main(). Also the return type should be int. Commented Feb 9, 2019 at 13:42
  • Also, you should avoid casting by using proper type and if you really need some cat, then you should use C++ cast like static_cast. Commented Feb 9, 2019 at 14:01
  • std::size(x) will only works when std::end(x) - std::begin(x) works so using C++ 17 would not help. C style arrays do not carry their size across function as the decay to pointers. Commented Feb 9, 2019 at 14:06

1 Answer 1

2

Implement std::size yourself:

template <typename T, std::size_t N>
constexpr auto size(const T(&)[N]) {
    return N;
}

usage:

int main() {
    int arr[] = {1, 2, 3};

    std::cout << "size: " << size(arr);
}

Note that you need to pass a reference to the array, since simply passing T[] will actually make you pass a T* to the first element of the array. That will not preserve any infromation regarding the array's size.

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

3 Comments

Note that it doesn't chain. You can't have main call void DoSomething(int arr[]) { int sz = size(arr); ... }.
Correct, but the OP seemed like he wanted to implement getArraySize and mentioned that he wants std::size. My note at the bottom should somehow indicate that it won't work if you pass T[], since passing T[] is pretty much the same as passing T*. I also emphasised the usage of a reference to an array to make that point clear.
Your post is great, just that the follow up question will be why it doesn't work in the case I mentioned. Then the response will be "You should be using std::vector" (which is what should be used in almost every case in C++ instead of a C array). Which I know you know. :-)

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.