1. In C language, after defining an array, you can use the sizeof command to obtain the length of the array (the number of elements it can accommodate).
For example: int?data[4];
int?length;
length=sizeof(data)/sizeof(data[0]);? //The total memory space occupied by the array is divided by the memory space occupied by a single element
printf("length?of?data[4]=%d",?length?);?//Output length? of?data[4]=4
2. However, it is not feasible to obtain the array length by passing the array name parameter to the sub-function.
For example: int?getLength(int[]?a){
int?length;
length=sizeof(a)/sizeof(a[0 ]);?//This is wrong, the result is always 1
return?length;
}
Because a is a function parameter, In this function, a is just a pointer (address. When this function is running, the system does not know how much data storage space the address represented by a has. Here we just tell the function: the first address of a data storage space), so, sizoef The result of (a) is the size of the memory occupied by the pointer variable a, which is generally 4 bytes on a 32-bit machine. a[0] is of type int, and sizeof(a[0]) is also 4 bytes, so the result is always 1.
3. Therefore, to obtain the array length, the effect can only be achieved by using the above method in the code area where the array is defined.