请澄清一下 [英] A clarification please

查看:63
本文介绍了请澄清一下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

常见问题1.7显示,除其他外,


int i = 0;


是一个定义。


K& R的第128页说:


"结构声明定义了一种类型。右边的大括号终止

成员列表后面可能跟一个变量列表,就像任何基本类型的

一样。也就是说,


struct {.....} x,y,z:


在语法上类似于


int x,y,z;


,因为每个语句都声明x,y和z为变量

命名类型**并导致为他们留出空间**。 {My

强调}


我的理解是定义会留出空间,

(例如int i = 0)但不是上面例子中的声明(例如int i)和x,y和z

。如何调和这两个想法?


像往常一样感谢。

FAQ 1.7 shows that, amongst other things,

int i = 0;

is a definition.

Page 128 of K&R say:

"A struct declaration defines a type. The right brace that terminates
the list of members may be followed by a list of variables, just as
for any basic type. That is,

struct {.....} x, y, z:

is syntactically analogous to

int x, y, z;

in the sense that each statement declares x, y, and z to be variables
of the named type **and causes space to be set aside for them**. {My
emphasis}

My understanding was that a definition causes space to be set aside,
( eg int i = 0) but not a declaration ( eg int i) and the x, y, and z
in the above example. How can these two ideas be reconciled?

Thanks as usual.

推荐答案

mdh写道:
mdh wrote:

常见问题1.7显示,除其他外,


int i = 0;


是一个定义。


K& R的第128页说:


"结构声明定义了一种类型。右边的大括号终止

成员列表后面可能跟一个变量列表,就像任何基本类型的

一样。也就是说,


struct {.....} x,y,z:


在语法上类似于


int x,y,z;


,因为每个语句都声明x,y和z为变量

命名类型**并导致为他们留出空间**。 {My

强调}


我的理解是定义会留出空间,

(例如int i = 0)但不是上面例子中的声明(例如int i)和x,y和z

。这两个想法如何协调?
FAQ 1.7 shows that, amongst other things,

int i = 0;

is a definition.

Page 128 of K&R say:

"A struct declaration defines a type. The right brace that terminates
the list of members may be followed by a list of variables, just as
for any basic type. That is,

struct {.....} x, y, z:

is syntactically analogous to

int x, y, z;

in the sense that each statement declares x, y, and z to be variables
of the named type **and causes space to be set aside for them**. {My
emphasis}

My understanding was that a definition causes space to be set aside,
( eg int i = 0) but not a declaration ( eg int i) and the x, y, and z
in the above example. How can these two ideas be reconciled?



注意你的定义概念是不对的。


明确初始化的声明变量的值是

总是一个定义。文件范围声明

使用关键字''static''或任何其他不使用

关键字''extern''的声明也是如此。最后一个案例涵盖''int i;''当它发生在

块范围时。


但是,所有其他变量声明都被认为是暂定的。 ,

包括int i;如果它发生在文件范围内,那就是它b / b
变得棘手。如果暂定定义之后是同一个变量名称的后续

声明,其中包含

初始化程序或使用关键字静态,暂定的定义

成为一个真正的定义。如果在翻译单元结束时,

没有变量的非暂定定义,则

暂定定义变为真实定义,除非声明

''extern'',在这种情况下,它变成了一个变量的声明,其实际定义在别处。

By noting that your concept of what a definition is isn''t quite right.

A declaration that explicitly initializes the value of a variable is
always a definition. The same is true of a file scope declaration
using the keyword ''static'', or any other declaration that does not use
the keyword ''extern''. That last case covers ''int i;'' when it occurs at
block scope.

However, all other variable declarations are considered "tentative",
including "int i;" if it occurs at file scope, and that''s where it
gets tricky. If a tentative definition is followed by a later
declaration of the same variable name in the same scope that includes
an initializer or uses the keyword ''static'', the tentative definition
becomes a real definition. If, by the end of the translation unit,
there are no non-tentative definitions of a variable, then the
tentative definition becomes a real one, unless it was declared
''extern'', in which case it becomes a declaration of a variable who''s
actual definition lies elsewhere.


2008年8月18日星期一15:23:06 -0700(PDT),mdh< md ** @ comcast.net>

写道:
On Mon, 18 Aug 2008 15:23:06 -0700 (PDT), mdh <md**@comcast.net>
wrote:

>常见问题1.7显示,除其他外,

int i = 0;

是一个定义。

K& R的第128页说:

"结构声明定义了一种类型。终止成员列表的右大括号后面可能跟一个变量列表,就像任何基本类型的
一样。也就是说,

struct {.....} x,y,z:

在语法上类似于

int x,y, z;

从某种意义上说,每个语句都声明x,y和z是命名类型**的变量,并导致为它们留出空间**。 {My
强调}

我的理解是定义会导致空间被搁置,(例如int i = 0)但不是声明(例如int i)和上例中的x,y和z
。这两个想法如何协调?
>FAQ 1.7 shows that, amongst other things,

int i = 0;

is a definition.

Page 128 of K&R say:

"A struct declaration defines a type. The right brace that terminates
the list of members may be followed by a list of variables, just as
for any basic type. That is,

struct {.....} x, y, z:

is syntactically analogous to

int x, y, z;

in the sense that each statement declares x, y, and z to be variables
of the named type **and causes space to be set aside for them**. {My
emphasis}

My understanding was that a definition causes space to be set aside,
( eg int i = 0) but not a declaration ( eg int i) and the x, y, and z
in the above example. How can these two ideas be reconciled?



纠正您的理解。


int i;是一个定义。它会导致对象i留出空间(在某些特定的短语意义上)。如果它没有b $ b,那么代码如i = j + k;没有地方存储结果。


extern int i;是宣言。它承诺我实际上在其他地方定义了

,链接器将能够解析它的位置。


struct t {...};是宣言。它没有定义一个对象。它

确实定义一种新型。 (我更喜欢说它声明类型但是

很难与K& R争论。)无论如何,定义一个类型与定义不同的是
对象或功能。 struct t {...} x;是一个

的定义。它定义了对象x并为其保留了空间。


所有这些都证明了单词define。从它衍生的单词

在用于描述对象的创建时意味着不同的东西,或者用于描述该语言其他方面的



这不是那么不寻常。单词token在谈论预处理器,语言语法或使用

标准函数strtok时,意味着不同的东西

。在尝试确定具体含义时,上下文非常重要。


-

删除电子邮件的del

By correcting your understanding.

int i; is a definition. It causes space to be set aside (in some
implementation specific sense of the phrase) for object i. If it
didn''t, code such as i = j+k; would have no place to store the result.

extern int i; is a declaration. It promises that i is in fact defined
somewhere else and the linker will be able to resolve its location.

struct t {...}; is a declaration. It doesn''t define an object. It
does "define" a new type. (I prefer to say it declares the type but
it''s hard to argue with K&R.) In any event, defining a type is
different than defining an object or function. struct t {...} x; is a
definition. It defines the object x and reserves space for it.

All of which proves that the word "define" and words derived from it
mean different things when used to describe the creation of objects or
used to describe other aspects of the language.

This is not that unusual. The word "token" means different things
when talking about the preprocessor, language syntax, or the use of
the standard function strtok. Context is important when trying to
decide what things mean.

--
Remove del for email


8月18日,4:40 * pm,jameskuy ... @ verizon.net写道:
On Aug 18, 4:40*pm, jameskuy...@verizon.net wrote:

mdh写道:
mdh wrote:

常见问题1.7显示,除其他外,
FAQ 1.7 shows that, amongst other things,


int i = 0;
int i = 0;


是一个定义。
is a definition.


K& R的第128页说:
Page 128 of K&R say:


"结构声明定义了一种类型。右边的大括号终止

成员列表后面可能跟一个变量列表,就像任何基本类型的

一样。也就是说,
"A struct declaration defines a type. The right brace that terminates
the list of members may be followed by a list of variables, just as
for any basic type. That is,


struct {.....} * x,y,z:
struct {.....} *x, y, z:


在语法上类似于
is syntactically analogous to


int x,y,z;
int x, y, z;


,因为每个语句都声明x,y和z是变量

命名类型**并为他们留出空间**。 * {My

强调}
in the sense that each statement declares x, y, and z to be variables
of the named type **and causes space to be set aside for them**. *{My
emphasis}


我的理解是定义会导致空间被搁置,

(例如int i = 0)*但不是声明(例如int i)以及上例中的x,y和z

。这两个想法如何协调?
My understanding was that a definition causes space to be set aside,
( eg int i = 0) *but not a declaration ( eg int i) and the x, y, and z
in the above example. How can these two ideas be reconciled?



注意你的定义概念是不对的。


明确初始化的声明变量的值是

总是一个定义。文件范围声明

使用关键字''static''或任何其他不使用

关键字''extern''的声明也是如此。最后一个案例涵盖''int i;''当它发生在

块范围时。


但是,所有其他变量声明都被认为是暂定的。 ,

包括int i;如果它发生在文件范围内,那就是它b / b
变得棘手。如果暂定定义是*后面的同一个变量名的后来

声明在同一范围内,包括

初始值设定项或使用关键字''static'',暂定的定义

成为一个真正的定义。如果在翻译单元结束时,

没有变量的非暂定定义,则

暂定定义变为真实定义,除非声明

''extern'',在这种情况下,它变成了一个变量的声明,它的实际定义位于其他地方。


By noting that your concept of what a definition is isn''t quite right.

A declaration that explicitly initializes the value of a variable is
always a definition. The same is true of a file scope declaration
using the keyword ''static'', or any other declaration that does not use
the keyword ''extern''. That last case covers ''int i;'' when it occurs at
block scope.

However, all other variable declarations are considered "tentative",
including "int i;" if it occurs at file scope, and that''s where it
gets tricky. If a tentative definition is *followed by a later
declaration of the same variable name in the same scope that includes
an initializer or uses the keyword ''static'', the tentative definition
becomes a real definition. If, by the end of the translation unit,
there are no non-tentative definitions of a variable, then the
tentative definition becomes a real one, unless it was declared
''extern'', in which case it becomes a declaration of a variable who''s
actual definition lies elsewhere.



我的脑袋旋转! :-)


感谢您的解释。

My head is spinning!! :-)

Thank you for that explanation.


这篇关于请澄清一下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆