C语言中如何用extern声明指针?
我正在为微控制器编写C软件,编译器是Microchip MCC18。
出于测试目的,我编写了一个非常简单的程序,如下所示:
.c 文件
#include "x.h"
char *now_clock;
.h 文件
extern char *now_clock;
使用上面的代码我得到一个语法错误,但我不知道出了什么问题。有什么帮助吗?
I am writing a C software for a microcontroller, the compiler is Microchip MCC18.
For test purposes, I have written a very simple program as follow:
.c file
#include "x.h"
char *now_clock;
.h file
extern char *now_clock;
With the above code I get a syntax error, but I don't know what is wrong. Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显示的代码在整个编辑过程中似乎没有发生变化,在严格警告下在 MacOS X 10.6.6 和 GCC 4.5.2 上正确编译 - 事实上它应该这样做。
xc 中的代码显示正确的样式 - 它包含声明变量的标头交叉检查变量的定义。您还可以毫无问题地向 xc 中的定义添加初始值设定项。
我的结论是,您过于简化了示例,并在此过程中丢失了问题。
The code shown, which doesn't seem to have changed across the edit, compiles correctly for me on MacOS X 10.6.6 with GCC 4.5.2 under stringent warnings - as indeed it should do.
The code in x.c shows correct style - it includes the header that declares the variable to cross-check the definition of the variable. You can also add an initializer to the definition in x.c without problems.
I conclude that you have over-simplified your example and lost the problem in the process.
您不在 .c 文件中声明
extern char* now_clock
,这就是您通过在 .c 中包含 xh 所做的事情。删除#include "xh"
就可以了。仅在想要访问该变量的 .c 文件中包含 xh。
You don't declare
extern char* now_clock
in the .c file which is what you are doing by including x.h in your .c. Remove#include "x.h"
and you'll be fine.Only include x.h in the .c files that want to access the variable.