howa said:
// example
#include <iostream>
using namespace std;
size_t bsearch(int *a) {
Here a is of type pointer-to-int. The fact that the int pointed to
happens to be the first in a series of ints that are stored in an array
in the calling function is irrelevant to the type of a here.
This statement calls the operator<< function overloaded for pointers
because a is a pointer.
cout<<sizeof(a) / sizeof(a[0])<<endl;
a is of type pointer-to-int
a[0] is of type int
This statement will output the size of a pointer-to-int divided by the
size of an int, whatever that is in your implementation.
return 1;
}
int main() {
int a[] = {1,3,5,7,9,11};
Here a is an array of 6 ints
This statement calls the operator<< function overloaded for pointers
because a decays from an array to a pointer in the context of the call
to the operator<< function. So you see the same result as inside the
bsearch function.
cout<<sizeof(a) / sizeof(a[0])<<endl;;
a has type array-of-6-ints and in the context of a sizeof expression
the array name does _not_ decay to a pointer as it did in the call to
operator<< above. So sizeof a is the size of the whole array, 6 x the
size of int. a[0] is of type int so this statement will output (6 x the
size of an int) divied by the size of an int, i.e. the output will be
6.
cout<< bsearch(a);
}
why they are difference ?
a has the same value, but why sizeof(a) / sizeof(a[0]) is difference?
Because in one case a is an array of six ints and in the other case a
is a pointer and those two entities are not the same size. (In both
cases a[0] is an int so is the same size in each case)
Gavin Deane