I want to make a function in C++ that returns size of any array using pointers:
*(&array + 1) - array
.
like this:
#include <iostream>
using namespace std;
void arrayLength(string array[]) {
int arraySize = *(&array + 1) - array;
cout << "Size of the array: " << arraySize << endl;
}
int main ()
{
string myArray[] = {"a", "b", "c", "d"};
arrayLength(myArray);
// Output: It seems to return the size in bytes (different numbers like this: 381286345)
return 0;
}
And this Code works:
#include <iostream>
using namespace std;
int main () {
string array[] = {"a", "b", "c", "d"};
cout << *(&array + 1) - array << endl;
return 0;
}