In general, do not apply the sizeof operator to the pointer when getting the length of an array .
Now take a look at the following code:
void clear (int array[]) {for
(size_t i = 0; i < sizeof (array)/sizeof (array[0)); i++) {
array[i] = 0;
}
}
void DoWork (void) {
int dis[12];
Clear (dis);
/*...*/
}
Clear () uses sizeof (array)/sizeof (Array[0]) to determine the number of elements in this array, but since array is a parameter, it is a pointer type, sizeof (array) = sizeof (int *) = 4 (32-bit OS)
When the sizeof operator is applied to a parameter that declares an array or function type, it produces the length of the adjusted (pointer) type
The solution to this problem is as follows:
void clear (int array[], size_t len) {for
(size_t i = 0; i < len; i++) {
array[i] = 0;
}
}
void DoWork (void) {
int dis[12];
Clear (Dis, sizeof (DIS)/sizeof (dis[0));
/*...*/
}