-
array == pointer ?
howdy~
i heard that array are actually a pointer, so i test it and all seems that remarks told the truth like this:
Code:
int arr[100];
std::cout << arr << std::endl;
std::cout << &arr << std::endl;
std::cout << &arr[0] << std::endl;
all three of them have the same output, which make me believe arr is absolutely a pointer itself. but if we declare a dynamic array this time, just like this:
Code:
int n = 100;
int* arr = new int(n);
std::cout << arr << std::endl;
std::cout << &arr << std::endl;
std::cout << &arr[0] << std::endl;
to my surprise this time output is different from the former, here arr and &arr[0] are still be equivalent, but &arr show me another result, if the assumption which regards array as pointer didnt go wrong then the output for &arr and arr may be the also the same because they are all points to same area, what's wrong here ??? very confused... :(
-
When you declare array like this:
int array[100];
name array is actually pointer to the first element of that array.
When you say:
int *array=new int (n);
that is same like
int *array;
array=new int(n);
that means array is a pointer (variable just like other has it's value and adress).
int *array will create variable (pointer) on some address say it is 0012FD44. And it' s value will be address of some other variable because it is pointer.
To simplify our vaariable (pointer array) will be placed on adress
0012FD44 and after array=new int(n);
it value will be adress of first byte of allocated memory for example 003708F8
Code:
0012FD44|003708F8|--------------->003708F8 ....etc
So in first case compiler reserve 100*sizeof(int) bytes of memory where name array is actually pointer on the first element of first byte,
in the second case you have two things:
-first compiler create variable which is pointer variable it has it's own adress
-second you dinamically allocate memory and new returns adress that will be stored in your variable array.
I'm not sure if I make this clear to you, because English is not my native language, but you must understand that int array[], and allocating with new or malloc are not equivalent things.
-
int array[7];
to the compiler array is an immedeate value of type address of int. That is
Code:
int array[7];
int a = 7;
int *p = array;
in this case 7 is an int, but it is not a variable of type int. The variable of type int is assigned the value 7, in exactly the same way as the pointer to int is assigned the value array. There is no spoon.
-
>i heard that array are actually a pointer
You heard wrong. Arrays are often converted to a pointer to the first element, but in an object context, the two are different.
>what's wrong here ???
Your understanding of context. The Rule states that in value context an array name is converted to a pointer to the first element of the array. This is why subscripting is often compared to dereferencing a pointer offset. It's an expression with value context, so a[i] really does become *(a + i). Likewise, a function call is also a value context, so you can pass an array to a function that expects a pointer to the appropriate type:
Code:
void
foo(
int *p
)
{
cout<< *p <<endl;
}
int
main()
{
int a[] = {1,2,3,4,5};
foo(a);
}
So an array becomes a pointer to the first element of the array when in value context. Value context is everything except for two cases:
1) As an operand to the sizeof operator.
2) As an operand to the address-of operator.
For arrays, a third case where a string literal is used as an initializer for an array of char also exists. Arrays are not pointers or constant pointers or anything like that, they're arrays. However, in practical use arrays are closely related to pointers simply because they become pointers much of the time.