0

The title might seem it's a duplicate question. Not sure if it actually is.

Consider the code below:

# include <iostream>

size_t arrSize(const int* arr)
{
    return *(&arr+1)-arr;
}

void foo(int* arr)
{
    std::cout << arrSize(arr);
}

int main()
{
    int arr[]{3, 6, -9, 52, -68};
    foo(arr);
    return 0;
}

It prints 4294967293 in my machine. I was expecting 5. I want an in-depth answer.

SuperNoob
  • 170
  • 1
  • 10
  • 2
    *I was expecting 5* -- Why? What gave you the impression that you can use a pointer, and magically figure out how many contiguous elements there are by just having a simple pointer? – PaulMcKenzie May 15 '21 at 15:00
  • 1
    `const int* arr` is a pointer. Full stop. Yes, in this code it happens to point at the first element of an array, but that doesn't make it an array. – Pete Becker May 15 '21 at 15:19

0 Answers0