main.c #include "main.h" const Byte array1[] = "sample string"; main.h #define ARRAY_SIZE sizeof(array1) file2.c #include "main.h" extern const Byte array1[]; for (i=0; i<ARRAY_SIZE; i++) { Function(array1[i]); }
已解决! 转到解答。
main.h: extern const Byte array1[14]; The disadvantage is obviously that you have to maintain the array size explicitly. The compiler will check the size at least when he sees the declaration and the definition while compiling main.c (say when the extern declaration is in main.h) -define another constant with the size. main.h: extern const Byte array1[]; extern const size_t array1_size; #define ARRAY_SIZE array1_size main.c: #include "main.h" const Byte array1[] = "Sample"; const size_t array1_size = sizeof(array1); The disadvantage here is that the code is a little bit less efficient as the array size is no longer known at compile time, but has to be actually read from a constant at runtime instead. Daniel