C 多个单行声明

发布于 2024-09-09 02:31:26 字数 161 浏览 4 评论 0原文

当我在一行上声明多个变量时会发生什么?例如

int x, y, z;

全部都是整数。问题是下面语句中的 y 和 z 是什么?

int* x, y, z;

都是int指针吗?

What happens when I declare say multiple variables on a single line? e.g.

int x, 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花心好男孩 2024-09-16 02:31:27

只有x是指向int的指针; yz 是常规整数。

这是 C 声明语法中让一些人感到困惑的一个方面。 C 使用声明符的概念,它引入了所声明的事物的名称​​以及类型说明符未提供的附加类型信息。在声明中,

int* x, y, z;

声明符是 *xyz (这是 C 语法的一个意外,您可以编写 int * xint *x,这个问题是我推荐使用第二种风格的几个原因之一)。 xyz 的 int 性由类型说明符 int 指定,而指针x 的 -ness 由声明符 *x 指定(IOW,表达式 *x 的类型为 int) 。

如果您希望所有三个对象都是指针,您有两种选择。您可以显式地将它们声明为指针:

int *x, *y, *z;

或者您可以为 int 指针创建 typedef:

typedef int *iptr;
iptr x, y, z;

只需记住,声明指针时,* 是变量名称的一部分,而不是类型。

Only x is a pointer to int; y and z are regular ints.

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;

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;

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.

初见你 2024-09-16 02:31:27

在你的第一句话中:

int x, y, z;

它们都是int

然而,在第二个中:

int* x, y, z;

只有 x 是指向 int 的指针。 yz 是普通的 int

如果您希望它们全部成为指向 int 的指针,您需要执行以下操作:

int *x, *y, *z;

In your first sentence:

int x, y, z;

They are all ints.

However, in the second one:

int* x, y, z;

Only x is a pointer to int. y and z are plain ints.

If you want them all to be pointers to ints you need to do:

int *x, *y, *z;
叶落知秋 2024-09-16 02:31:27

只有 x 是 int 指针。 Y 和 Z 将只是 int。
如果你想要三个指针:

int * x, * y, * z;

Only x is an int pointer. Y and Z will be just int.
If you want three pointers:

int * x, * y, * z;
过期情话 2024-09-16 02:31:27

重要的是要知道,在 C 中,声明模仿了用法。 * 一元运算符在 C 中是右关联的。因此,例如,在 int *x 中,x 是指向 int(或 int-star)的指针类型,而在 int x 中>, x 是 int 类型。

正如其他人也提到的,在 int* x, y, z; 中,C 编译器将 x 声明为 int-star,将 y 和 z 声明为整数。

It is important to know that, in C, declaration mimics usage. The * unary operator is right associative in C. So, for example in int *x x is of the type pointer to an int (or int-star) and in int x, x is of type int.

As others have also mentioned, in int* x, y, z; the C compiler declares x as an int-star and, y and z as integer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文