我有我的长度函数来计算数组的长度,但它给出了两个多余的垃圾数(负数)。它必须返回 6,但由于垃圾值而返回 8。
#include<stdio.h>
int length(int *arr) {
int _length = 0;
while (*arr) {
_length++;
arr++;
}
return _length;
}
int main() {
int arr[] = {2, 1, 3, 4, 5, 6};
printf("%d\n", length(arr));
return 0;
}
Run Code Online (Sandbox Code Playgroud)