How to define an array of strings of characters in header file?

后端 未结 5 456
眼角桃花
眼角桃花 2021-02-04 04:05

I have many different 3 axis sensors I am writing test code for. In the C files for each of them, I have the same char string defined:

char axis[3][8] = {\"X\",          


        
5条回答
  •  野性不改
    2021-02-04 05:03

    In order to avoid linker errors, you have to declare your array as extern in a header file, and then define the array once in one of your code modules.

    So for instance:

    //myheader.h
    extern const char* axis[3];
    

    then in another code module somewhere:

    //myfile.c
    const char* axis[3] = { "X", "Y", "Z" };
    

提交回复
热议问题