Hi, There is a Q&A in keil database named "GENERAL: DECLARING VARIABLES IN HEADER FILES" It shows how to declare single variables & initialize them in a header file. When I use the same method for initializing an array the compiler generates a warning: "macro '_INIT': parameter count mismatch" my code is: _DECL unsigned char ToneDisp[16] _INIT({'D','1','2','3','4','5','6','7','8','9','0',0x2A,0x23,'A','B','C'}); How should I initialize the array? The document mentioned above doesn't recommend this.What are the alternative solutions? Thanks for your attention in advance A.E.
I would recommend against allocating storage in a header file. Some programmers like to have a dual-purpose header file which can serve as either declaration or definition (.h or .c), usually depending on a magic #define you have to set before #including in the .c. The argument for doing so is to have just one place to maintain a declaration. I would put the extern reference into the header file, and in the corresponding .c file, I would put the full definition, including the initializer. No macro necessary. Yes, the declaration then appears in two places, but to me this is a minor matter compared to trying to do preprocessor tricks just to declare a variable. The array size (16) also deserves some sort of definition for a symbolic constant.
.h: #define NumDtmfTones 16 extern unsigned char ToneDisp[NumDtmfTones]; .c: unsigned char ToneDisp[NumDtmfTones] = { 'D', '1', '2', '3', '4', '5', '6', '7', '8', '9','0', 0x2A, 0x23, 'A', 'B', 'C'};
"I would put the extern reference into the header file, and in the corresponding .c file, I would put the full definition, including the initializer. No macro necessary. Yes, the declaration then appears in two places, but to me this is a minor matter compared to trying to do preprocessor tricks just to declare a variable." I would agree with you. However, I would add one point: be sure to include the .h file into the .c file that has the full definition (with initialiser); that way, the compiler will warn you when the the declaration in the .h file gets out-of-step with the definition in the .c file! You don't get this protection with a single file and Magic Macros! For example: file.h
extern int fred;
#include "file.h" int fred = 3;
I meant to add that this is all bog-standard, pure vanilla 'C' - nothing specifically to do with Keil or the 8051 at all! Time to get out the 'C' text book, then.