如何在头文件中定义字符串数组? [英] How to define an array of strings of characters in header file?

查看:703
本文介绍了如何在头文件中定义字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多不同的3轴传感器,它们正在为其编写测试代码.在每个文件的C文件中,我都定义了相同的char字符串:

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", "Y", "Z"}

在"for"循环结果中使用该结果来打印失败的轴,如下所示:

which I use when I "for" loop results to print the axis that is failing like this:

DEVICETEST_LOG("%s Failed %s axis for Min range\n",device_name[DUT], axis[i]);

我当时想节省一些空间,我可以在头文件中定义一个字符串数组以在所有地方使用.

I was thinking to save some space I could define a character string array in a header file to use all over the place.

我已经尝试了很多方法,但是似乎无法在头文件中定义一个字符串数组,可以迭代该字符串以传递编译器.

I have tried a number of things, but I can't seem to get an array of strings defined in my header file that I can iterate through to pass a compile.

推荐答案

为避免链接程序错误,您必须在头文件中将数组声明为extern,然后一次定义数组 在您的一个代码模块中.

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.

例如:

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

然后在某个地方的另一个代码模块中:

then in another code module somewhere:

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

这篇关于如何在头文件中定义字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆