intMain () {intNumber[] = {1,2,3,4,5 }; int*pnumber =Number ; cout<<sizeof(number) <<Endl; cout<<sizeof(Pnumber) <<Endl; System ("Pause"); return 0;}
In a 32-bit system, the pointer size is 4 bytes, and the output is:
So number is not a pointer.
But the array name can also be assigned directly to the pointer:
The array name cannot be self-decrement:
The array name is a pointer constant .
(1) The connotation of the array name is that its reference entity is a data structure, and this data structure is an array;
(2) The extension of the array name is that it can be converted to a pointer to its reference entity, and is a pointer constant;
(3) A pointer to an array is another variable type (under the WIN32 platform, length 4), which means only the storage address of the group!
- C + + Array Parameters Why must you specify the length of the array?
Take a look at the following example:
#include <iostream>using namespacestd;intMaxintA[],intsize);//size is an array of sizesintMain () {intNumber[] = {2, $, A,6, at,98, -,3 };cout << Max (number,sizeof(number)/sizeof(int)) <<Endl; System ("Pause"); return 0;}intMaxintA[],intsize) { intMax =0; for(inti =0; i<size; i++) { if(a[i]>max) Max=A[i]; } returnMax;}
At the end of the definition array "int number[] = { 2, 6, 3 , 98 Then, if there is a sentence: "cout << number[8] << Endl;" is not judged to be out of bounds, but output:
The array is an initial address, and the function passing mechanism of C + + is not responsible for the end of the following array.
Then look at the memory:
The so-called array simply stores the data sequentially in memory, and the program simply extracts the data sequentially from the first address of the array, and when you are larger than the array length, the program does not know it, but still takes the contents of the back address ("CCCCCCCC") as data out.
When the array is passed as a parameter to a child function, only the quilt function resolves to an address:
If the value of output A is:
To cite one example:
#include <iostream>using namespacestd;voidTestarrayarg (inta[]) {cout<<Endl; cout<<"In func ..."<<Endl; cout<<"Array Address:"<< a <<Endl; cout<<"Array Size:"<<sizeof(a) <<Endl; cout<<"array element count:"<<sizeof(a)/sizeof(a[0]) <<Endl; cout<<"changing the 4th element ' s value to ten."<<Endl; a[3] =Ten; //With 0x0042f980 as the first address, modify the value of the third data }intMain () {intNumber[] = {1,2,3,4,5 }; cout<<"In main ..."<<Endl; cout<<"Array Address:"<< number <<Endl; cout<<"Array Size:"<<sizeof(number) <<Endl; cout<<"array element count:"<<sizeof(number)/sizeof(number[0]) <<Endl; Testarrayarg (number); cout<< Endl <<"The 4th element ' s value:"<< number[3] <<Endl; System ("Pause"); return 0;}
Output Result:
In fact, the value of modify a[3], Number[3] is also changed, because the array A and number of numbers are the same address, the same address, modify the contents of the same addresses, number is resolved to an array, a is parsed as a pointer, but both can parse the data backwards.
And in terms of the data of the Func output of the child function, a is parsed into an element, a size of 4 bytes, that is, when the array number is passed to a, a is just a pointer, and a pointer does not know how many data will be in the future from the beginning. Therefore, if you access the elements of an array in a function that accepts array parameters, you pass the array size as another auxiliary parameter in the domain scope that defines the array, lest the access be out of bounds when the pointer is used as an array.
We conclude that when an array is parameterized, it is degraded to a pointer, and when an array is used as an argument, the array size is passed in . where max (int a[], int size) is equivalent to Max (int*a, int size) is also equivalent to Max (int a[10], int size), Anyway just pass in a pointer, don't care how to "decorate" a.
Reference: Http://www.cnblogs.com/viviwind/archive/2012/08/16/2642535.html, where the multidimensional array is also described in brief.
- Declare an array as a parameter when you are referencing a group:
We discussed earlier that when an array is passed as a parameter to a child function, the array is degraded to a pointer, the child function does not know the length of the array passed to him, and the compiler does not know that the length of the array is not checked when the compiler checks the parameter types. However, when a parameter is a reference to an array type, both the declaration and the definition must indicate the size of the parameter array, otherwise an error is prompted:
Also, if you attempt to pass in an array that is not 8 in size, you will also be prompted with an error:
We use the array reference as a parameter to prevent the array from being passed as a parameter to a child function when it is degraded to a pointer .
Modify the previous example:
#include <iostream>using namespacestd;voidTESTARRAYARG1 (inta[]) {cout<<Endl; cout<<"In func1 ..."<<Endl; cout<<"Array Address:"<< a <<Endl; cout<<"Array Size:"<<sizeof(a) <<Endl; cout<<"array element count:"<<sizeof(a)/sizeof(a[0]) <<Endl;}voidTESTARRAYARG2 (int(&a) [5]) {cout<<Endl; cout<<"In func2 ..."<<Endl; cout<<"Array Address:"<< a <<Endl; cout<<"Array Size:"<<sizeof(a) <<Endl; cout<<"array element count:"<<sizeof(a)/sizeof(a[0]) <<Endl;}intMain () {intNumber[] = {1,2,3,4,5 }; cout<<"In main ..."<<Endl; cout<<"Array Address:"<< number <<Endl; cout<<"Array Size:"<<sizeof(number) <<Endl; cout<<"array element count:"<<sizeof(number)/sizeof(number[0]) <<Endl; TESTARRAYARG1 (number); TESTARRAYARG2 (number); System ("Pause"); return 0;}
After running we find that the main function and the FUNC2 result are the same:
A in func1 is interpreted as a pointer:
A in FUNC2 is still parsed as an array:
Reference: http://blog.csdn.net/jiangxinyu/article/details/7767065
- Array names and arrays of addresses:
intMain () {intNumber[] = {1,2,3,4,5 }; cout<< number <<Endl; cout<< &number <<Endl; cout<< number +1<<Endl; cout<< &number +1<<Endl; System ("Pause"); return 0;}
Operation Result:
A is the array name, which represents the address of the first element of the array; &a is the address of the entire array. The values for both outputs are the same and are 0x0032fb88, but they differ from one element to the other, and the latter from the point of view of the entire array. So a+1 just adds the size of an element, and &a+1 adds an array size. In fact, &a+1, &a+2 This form is more common in the operation of the two-dimensional array, because the two-dimensional array is the data member is an array of arrays, so the operation of one of the elements is an array of operations.
A few discussions on C + + arrays