我是C语言的新手,我想做的是定义一个具有恒定地址的指针,这是我在testHeader.h中所做的尝试:
char const *MY_VAR_P = (char * const)(0x4fffff);
extern char MYVAR;
然后在test.c的全局中(但是我想到要在其他.c文件中重用具有相同地址的相同指针...):
#include "testHeader.h"
char MYVAR = '0';
*MY_VAR_P = MYVAR;
这是错误:
test.c:33: warning: type defaults to `int' in declaration of `MY_VAR_P'
test.c:33: conflicting types for `MY_VAR_P'
test.h:2: previous declaration of `MY_VAR_P'
test.c:33: warning: initialization makes pointer from integer without a cast
test.c:33: initializer element is not constant
我真的不明白...请有人可以帮助我吗?
谢谢!
该行有两个问题:
MY_VAR_P
具有指向常量char的类型指针。您无法修改MY_VAR_P
通过该指针指向的内容。也许您希望该变量成为指向char的const指针?
char * const MY_VAR_P = ...;
您不能在函数外部运行代码。作业*MY_VAR_P = foo
需要放在您的main或其他函数中。(您使用的其他地方=
不是分配,而是初始化。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句