#define lengthOfArray(x) (sizeof(x)/sizeof(x[0]))
似乎在main()中給出了正確的答案,但是在array_set()的printf()中給出了正確的答案。我錯過了什麼?檢查數組的長度
int* decValue2[BUF_SIZE] = {
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
};
#define lengthOfArray(x) (sizeof(x)/sizeof(x[0]))
void array_set(int* y_dest, int* y_src, int arrayLength){ // arrays
int i = 0;
printf("lengthOfArray in function: %d \n", lengthOfArray(y_src));
// [TODO] check that y_dest has enough space
for (i=0;i<arrayLength; i++)
y_dest[i] = y_src[i];
//
}
// In main()
int y_int = 0;
int y_array[BUF_SIZE];
int_set_right(&y_int, 9876543);
array_set(y_array, decValue2, BUF_SIZE);
printf("lengthOfArray: %d \n", lengthOfArray(y_array));
printf("Value int :: %d \n",y_int);
int i = 0;
for (i=0;i<BUF_SIZE; i++)
printf("%x ", (0xff & y_array[i]));
[c,找到函數內數組的長度]的可能重複(http://stackoverflow.com/questions/17590226/c-finding-length-of-array-inside-a-function)。這個問題可能還有其他重複項 - 這是在「相關」列表中。事實上,這裏有一個7位數(較舊)的問題:[C編程語言中數組的大小](http://stackoverflow.com/questions/1975128/sizeof-an-array-in-the-c-programming-語言)。 –