When you declare an array as a parameter (whether you mention the
size or not) you receive a pointer to the first element of the
array you are passing. The size of a pointer in a 32 bit platform
like linux is 4 bytes.
Inside 'main' you are actually getting the size of the array
which is 3 * sizeof(int) = 3 * 4 = 12.
There is no way you can get the size of an array passed to a
function. You could use an extra parameter to indicate the size.
On Sat, Jan 09, 1999 at 08:05:45AM -0500, Yasushi Shoji wrote:
> hi all,
>
> i have a question about following code:
>
> -- from here --
>
> int i;
>
> void
> sub(int a[3])
> {
> i = sizeof(a);
> }
>
> int
> main(void)
> {
> int a[3];
> i = sizeof(a);
> sub(a);
> return 0;
> }
>
> -- till here --
>
> in the function sub i is 4,
> and in main, it's 12.
>
> <snip>