多种Ç一行声明 [英] C multiple single line declarations
问题描述
当我宣布说,在一行上多个变量会发生什么?例如。
What happens when I declare say multiple variables on a single line? e.g.
int x, y, z;
所有的都是整数。问题是什么是Y和Z在下面的语句?
All are ints. The question is what are y and z in the following statement?
int* x, y, z;
他们全部诠释指针?
Are they all int pointers?
推荐答案
只有 X
是一个指针为int; 是
和以Z
都是正规整数。
Only x
is a pointer to int; y
and z
are regular ints.
这是C的声明语法的一个方面绊倒一些人了。 C使用的不是类型说明符的提供的声明符的,介绍的东西的名字被宣布的以及附加的类型信息的概念。在声明
This is one aspect of C declaration syntax that trips some people up. C uses the concept of a declarator, which introduces the name of the thing being declared along with additional type information not provided by the type specifier. In the declaration
int* x, y, z;
的说明符 * X
,是
和以Z
(这是,你可以写为为int * X
或为int * X
,而这个C语法的事故问题是几个原因,我建议使用第二种风格)之一。的int-内斯 X
,是
和以Z
由类型说明符指定的 INT
,而 X
由声明符指定的<$ C $的指针岬C> * X (IOW,前pression * X
的类型 INT
)。
the declarators are *x
, y
, and z
(it's an accident of C syntax that you can write either int* x
or int *x
, and this question is one of several reasons why I recommend using the second style). The int-ness of x
, y
, and z
is specified by the type specifier int
, while the pointer-ness of x
is specified by the declarator *x
(IOW, the expression *x
has type int
).
如果您希望所有三个对象是指针,你有两个选择。您可以声明为指针明确:
If you want all three objects to be pointers, you have two choices. You can either declare them as pointers explicitly:
int *x, *y, *z;
或者您可以创建一个int指针的typedef:
or you can create a typedef for an int pointer:
typedef int *iptr;
iptr x, y, z;
请记住,将指针声明时, *
是变量名称的一部分,而不是类型。
Just remember that when declaring a pointer, the *
is part of the variable name, not the type.
这篇关于多种Ç一行声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!