I've been working my way slowly through on understanding pointers. One of the examples in the book shows how to use getSize to return the number of bytes in an array. When I looked at the function I figured rather than doing it in a function I could just add the * into sizeOf thinking that it should return the same result. But it returned 8 rather than 4, so I'm a bit confused on why it returned a different value. I was wondering what it actually returned and why it returned a different value?
Snippets of code:
1 2 3 4 5 6 7 8 9 10 11 12
int main()
{
double dArray[ 20 ];
cout << "The number of bytes in the array is " << sizeof (dArray );
cout << "\nThe number of bytes returned by getSize is " << getSize( dArray );
cout << "\ntesting here: " << sizeof( *dArray ) << endl;
}
size_t getSize( double *ptr )
{
returnsizeof( ptr );
}
The program's output is:
The number of bytes in the array is 160
The number of bytes returned by getSize is 4
testing here: 8