This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

DECLARING VARIABLES (arrays) IN HEADER FILES

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.

Parents
  • "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;
    file.c
    #include "file.h"
           int fred = 3;

Reply
  • "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;
    file.c
    #include "file.h"
           int fred = 3;

Children