We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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 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.