While it looks like you've declared sort() as taking as it's first
argument an array of int, this is just an illusion. Whenever a function
parameter is declared as if it were an array, that declaration is
adjusted by changing it into a pointer to the element type of that
array. In this case, int b[] gets adjust to "int *b".
As a result, inside sort(), "sizeof b" gives you the same result as
"sizeof(int*)" - it contains no information about the size of the array
that was passed to sort(). That's why sort() needs a second argument
that does indicate the size of the array.
It is clear now......
Inside sort function sizeof(b)=2 which is just sizeof(int*)
Keep in mind that sizeof(int*) is different on different systems; on
most of the systems I use, sizeof(int*) is 4.
Now how does sizeof() operator gets sizes of array e.g.
{
int a[3]={1,2,3};
printf("%d",sizeof(a));
}
It gives correct result 6...when sizeof(int)=2
but how does sizeof operator know what is size of array...as there is
not any array end marker as in case of string('\0') ..
Unlike most other operators, the sizeof operator doesn't evaluate the
value of of it's argument, it only pays attention to the type of it's
argument. That type is known at compile time. There's a tricky exception
that involves VLAs, but I don't think you need to go into that right now.
In most cases, the name of an array decays into a pointer to the first
element of that array. However, there are a few exceptions, and sizeof
is one of them. As a result, the type of 'a' is "int[3]", not "int*",
and that type contains all of the information sizeof needs to determine
the correct size for the array.