如何在文件之间共享字符串常量数组?
自从我实际编写直接 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它与变量相同:
您也可能需要
const char * const
,而不是const char const *
,这是无效的语法。It's the same as for variables:
Also you probably want
const char * const
, notconst char const *
which is invalid syntax.