Keith said:
CBFalconer <
[email protected]> writes:
When array is passed as a parameter, which this one isn't.
Sorry, you've blown this one.
An expression of array type is implicitly converted to a pointer to its
first element *unless*:
It's the operand of a sizeof operator, or
It's the operand of a unary "&" operator, or
It's a string literal used to initialize an array.
Passing it as a parameter is irrelevant.
The behavior of any one implementation doesn't prove anything, but
this works:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int array[5];
double another_array[10];
size_t s1 = sizeof *array;
size_t s2 = sizeof *another_array;
printf("s1 = %d, s2 = %d\n", (int)s1, (int)s2);
return 0;
}
and prints "s1 = 4, s2 = 8" on my system (matching sizeof(int) and
sizeof(double)).
Forgive me, but I still think CBFalconer is right. I think it's more of
a discussion related to whether an array is a const pointer or it's an
rvalue which makes it an address value which is what a pointer contains
but it's not a pointer object.
The first idea may be implementation dependent. I tried it with gcc.
Let's say you have int a[10] and try a++. Now, if it were a constant
gcc would complain: increment of read only variable. In this case it
says wrong type of argument to increment. This sounds like it's not a
const pointer but just the address of the pointer (which is correct
since &a, a and &a[0] have the same value, so the pointer (&a) has the
same address as it's value (a). It can be argued that this is
implementation dependent, I guess.
The second idea is to see how it works:
1).
int a[10];
a[2]=3;
2).
int *b=malloc(10*sizeof(int));
b[2]=3;
In the first case we have the address value a, so:
Step 1: Go to positions forward from a[0]
Step 2: At the address put 3
In the second case we have:
Step 1: Get the value contained in b
Step 2: Add two positions to it
Step 3: Set the value the the address 3.
I guess it can be argued that this is also implementation dependent
since you could get the address value of a from &a (since they're
equal) but I doubt that happens.
It seems it may be that the implementation dictates the choice of words
.
If the array is an rvalue, when passed to a function like f(int *arr),
arr will behave like a pointer in any case, independent of
implementation.