int a[1];a[1]=1;
This is an error. You are corrupting the heap.
if a is only 1 element big, then a[0] is the only element. a[1] is out of bounds.
The correct way to do this would be:
int a[1]; a[0] = 1;
Remember that array indexes always start at 0, not at 1.
cout<<&a<<a;
a gives you the variable.
&a gives you a pointer to the variable.
The tricky bit is that 'a' is an array, and arrays can be
implicitly cast to a pointer to their first element.
Therefore
cout << a;
is the same as
cout << &a[0];
. Both are pointers to the first element in the array.
So... that means:
- a is like a pointer to the first element in the array
- &a is a pointer to the array itself.
Since the first element of the array is at the same address as the array itself, the same value is printed to cout.
Therefore the only difference between '&a' and 'a' here is their type. &a points to an int[1] and 'a' points to an int.
and why
1 2 3 4 5 6 7
|
int main (void)
{
int (*p)[1];
int a[1];a[1]=1;
p=a;
getchar();
}
|
doesnt work?? |
You have a type problem.
'p' points to an int[1]
'a' points to an int
therefore you cannot do
p=a;
because you're assigning mismatching pointer types.
The correct way would be:
p=&a;
because &a is a pointer to int[1].