In file AAA.c
const char CMD_Fire[] = "FIRE";
In file BBB.c
extern const char CMD_Fire[];
It seems that, the compiler is not able to know the size of CMD_Fire[], when compiling BBB.c
So, I have to hard code the size of CMD_Fire[] in BBB.c
Then, if I change the command to "Fire it!", I will need to change the size of CMD_Fire[] everywhere.
Is there any skill to handle this?
The sizeof() operator should do the trick.
Alternatively, you use a #define to explicitly specify the length of the array, altough that may be somewhat bothersome if the length of the string is changed frequently.
No, it won't - not for an array of unspecified size like this!
See: c-faq.com/.../extarraysize.html
"The sizeof() operator should do the trick."
What - Used within bbb.c ?
Not sure that's the case. The compiler won't know the details of the external array when it is compiling bbb.c.
No, it won't - not for an array of unspecified size like this!<p>
I see .. I rarely use sizeof(), so I'm not aware of all of its limitations.
Another thing, though - the char array is initialized as a string, so it is terminated by a null ('\0') character. Therefore, the string functions (like strlen()) from the standard libary can be used.