In doing this problem:
32-bit environment, int *p=new int[10]; the value of sizeof (P) is ()
A, 4 B, ten C, D, 8
I thought the correct answer for the C,int type is 32 bits, four bytes, 10 natural is 40, the result is the correct answer is a, only the space of the pointer p.
So write a piece of code to test it:
1#include <iostream>2 using namespacestd;3 voidFunintP[])//p use as pointer here4 {5cout<<"in the function"<<sizeof(P) <<Endl;6 }7 intMain ()8 {9 inta[Ten];Ten int* b=New int[Ten]; Onecout<<"Array name"<<sizeof(A) <<Endl; Acout<<"Pointers"<<sizeof(B) <<Endl; - Fun (A); -}
Or
1#include <iostream>2 using namespacestd;3 void fun (int *P) 4 {5cout<<"in the function"<<sizeof(P) <<Endl;6 }7 intMain ()8 {9 inta[Ten];Ten int* b=New int[Ten]; Onecout<<"Array name"<<sizeof(A) <<Endl; Acout<<"Pointers"<<sizeof(B) <<Endl; - Fun (A); -}
Result output:
Array name 40
Pointer 4
In the function 4
This shows that the array name is not exactly the same as the pointer. Although they can all access the array by pointers.
However, arrays are degenerate into pointers as they are passed as function parameters. This is why the pointer is often a length when passed as a parameter. (WSJ Note: When the pointer is a formal parameter, it is usually accompanied by a formal parameter-the length of the pointer)
Transferred from: http://blog.csdn.net/kangroger/article/details/20653255
sizeof (array name) and sizeof (pointer)