为什么我不能将全局变量初始化为NULL? [英] Why can't I initialize a global variable to NULL?
问题描述
我有一个声明声明这样一个全局变量:
char * pname = NULL;
然后我用夹板检查代码,我得到错误:
err.c:8:15:全局pname初始化为空值:pname = NULL
没有null的引用注释被分配或初始化为
NULL。使用
/ * @ null @ * /将引用声明为可能为空的指针。 (使用
-nullassign来禁止警告)
err.c:8:15:全局pname初始化为空值:char * pname = NULL
= NULL
这是什么意思?
感谢任何帮助,谢谢。
I have a statement declares a globle variable like this :
char *pname = NULL;
Then I used splint to check the code, I got errors:
err.c:8:15: Global pname initialized to null value: pname = NULL
A reference with no null annotation is assigned or initialized to
NULL. Use
/*@null@*/ to declare the reference as a possibly null pointer. (Use
-nullassign to inhibit warning)
err.c:8:15: Global pname initialized to null value: char * pname = NULL
= NULL
What''s that mean?
Any help is appreciated, thanks.
推荐答案
moxm写道:
我有一个声明声明这样一个全局变量:
char * pname = NULL;
然后我用splint检查代码,我得到错误:
err.c:8:15:全局pname初始化为null值:pname = NULL
没有空注释的引用被分配或初始化为
NULL。使用
/ * @ null @ * /将引用声明为可能为空的指针。 (使用
-nullassign来禁止警告)
err.c:8:15:全局pname初始化为空值:char * pname = NULL
= NULL
这是什么意思?
感谢任何帮助,谢谢。
I have a statement declares a globle variable like this :
char *pname = NULL;
Then I used splint to check the code, I got errors:
err.c:8:15: Global pname initialized to null value: pname = NULL
A reference with no null annotation is assigned or initialized to
NULL. Use
/*@null@*/ to declare the reference as a possibly null pointer. (Use
-nullassign to inhibit warning)
err.c:8:15: Global pname initialized to null value: char * pname = NULL
= NULL
What''s that mean?
Any help is appreciated, thanks.
在C中,所有全局指针变量都保证初始化为
NULL,因此将pname设置为NULL是过度的。这可能就是Splint
试图说的(但是以一种奇怪的方式)。
八月
In C all global pointer variables are guaranteed to be initialized to
NULL, so setting pname to NULL is overkill. That probably is what Splint
is trying to say (but in a strange way).
August
akarl写道:
moxm写道:
moxm wrote:
我有一个声明声明一个这样的全球变量:
char * pname = NULL;
I have a statement declares a globle variable like this :
char *pname = NULL;
这是一个声明和一个定义,
但不是声明。
C全球保证指针变量初始化为
NULL,
That''s a declaration and a definition,
but not a statement.
In C all global pointer variables are guaranteed to be initialized to
NULL,
....除非代码将它们初始化为其他内容,
喜欢地址常数。
/ * BEGIN new.c * /
int global;
int * pointer =& global;
int main(void)
{
return * pointer; < br $>
}
/ * END new.c * /
-
pete
.... unless the code initializes them to something else,
like an address constant.
/* BEGIN new.c */
int global;
int *pointer = &global;
int main(void)
{
return *pointer;
}
/* END new.c */
--
pete
pete写道:
akarl写道:
akarl wrote:
moxm写道:
moxm wrote:
我有一个声明声明这样一个全局变量:
char * pname = NULL;
I have a statement declares a globle variable like this :
char *pname = NULL;
这是一个声明和定义,
但不是声明。
That''s a declaration and a definition,
but not a statement.
在C中,所有全局指针变量都保证初始化为
NULL,
In C all global pointer variables are guaranteed to be initialized to
NULL,
...除非代码将它们初始化为其他东西,
就像一个地址常数。
... unless the code initializes them to something else,
like an address constant.
叹息......
Sigh...
这篇关于为什么我不能将全局变量初始化为NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!