如何在文件之间共享字符串常量数组?

发布于 2025-01-03 03:30:29 字数 484 浏览 0 评论 0原文

自从我实际编写直接 c(甚至不是 C++,而是 c)以来已经很长时间了,并且我知道如何使用 extern 关键字在单独的 .c 文件之间共享变量,但我能做什么?不记得如何在文件之间共享常量数据?

例如,假设我有这个...(注意,这不是 c 代码(或者如果是的话,也是一个意外),而是显示我想要的内容的伪代码):

const char const * WEEKDAYS[] = {
    "Sunday",
    "Monday", 
    "Tuesday"
}

现在我'我试图创建一个指向数据的字符指针数组。再说一遍,这是常量数据,所以我想直接在标头中定义它,但这就是我无法弄清楚如何做到这一点的地方,或者如果这不是你应该这样做的方式,你仍然应该这样做在 c 文件中声明它,然后在其他地方包含的标头中使用 extern。

再说一次,自从我不得不处理这个问题以来已经很长时间了,感谢更新、更现代的语言,但希望你能提供帮助。

Been a loooooong time since I've actually coded straight c (not even C++ but c) and I know how to use the extern keyword to share a variable between separate .c files, but what I can't remember is how to share constant data between files?

For example, say I have this... (note, this is not c code (or if it is, its an accident) but rather pseudo-code to show what I want):

const char const * WEEKDAYS[] = {
    "Sunday",
    "Monday", 
    "Tuesday"
}

Now I'm trying to create an array of char pointers that point to the data. Again, this is constant data so I'd like to just define it in a header directly, but that's where I can't figure out how to do it, or if that isn't how you should do it anyway and you should still declare it in the c file, then use extern in the header you include elsewhere.

Again, been a long time since I've had to deal with this thanks to the newer, more modern languages, but hoping you can help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单 2025-01-10 03:30:29

它与变量相同:

// header
extern const char * const WEEKDAYS[3];

// implementation
const char * const WEEKDAYS[3] = {
    "Sunday",
    "Monday",
    "Tuesday"
};

您也可能需要 const char * const,而不是 const char const *,这是无效的语法。

It's the same as for variables:

// header
extern const char * const WEEKDAYS[3];

// implementation
const char * const WEEKDAYS[3] = {
    "Sunday",
    "Monday",
    "Tuesday"
};

Also you probably want const char * const, not const char const * which is invalid syntax.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文