I wanted to see if I could initialize a global variable to point to itself:
我想知道我是否可以初始化一个全局变量来指向它自己:
#include <stdio.h>
struct foo { struct foo *a, *b; } x = { &x, &x };
int main()
{
printf("&x = %p, x.a = %p, x.b = %p\n", &x, x.a, x.b);
return 0;
}
This code compiles and runs as expected with gcc
(all three pointers print identically).
这段代码按照预期使用gcc编译和运行(所有三个指针打印一致)。
I want to know:
我想知道:
- Is this reliable?
- 这是可靠的吗?
- Is this standard?
- 这是标准的吗?
- Is this portable?
- 这是便携式吗?
EDIT: Just to clarify, I am questioning the availability of the address of x
in its own initializer.
编辑:澄清一下,我在质疑x在它自己的初始化器中的可用性。
2 个解决方案
#1
8
This is standard C code.
这是标准的C代码。
This paragraph of the mighty Standard permits it (emphasis mine):
强有力的标准的这一段允许它(强调我的):
(C99, 6.2.1p7) "Structure, union, and enumeration tags have scope that begins just after the appearance of the tag in a type specifier that declares the tag. Each enumeration constant has scope that begins just after the appearance of its defining enumerator in an enumerator list. Any other identifier has scope that begins just after the completion of its declarator."
(C99 6.2.1p7)“结构、联合和枚举标记具有范围,范围从声明标记的类型说明符中的标记出现之后开始。每个枚举常数的作用域都在枚举数列表中定义枚举数出现后开始。任何其他标识符的作用域都是在声明符完成之后开始的。
For information, note that to illustrate the last sentence of 6.2.1p7, the book "The New C Standard" by Derek M. Jones uses an example similar to yours:
有关资料,请注意,德里克·琼斯(Derek M. Jones)的《新C标准》(the New C Standard)一书中,为了阐明6.1 .1p7的最后一句话,用了一个与你相似的例子:
struct T {struct T *m;} x = /* declarator complete here. */ {&x};
#2
8
Yes to all of the above. You have a couple of pointers that you're initializing with the same address, so they hold the same address, and that's the same as the address with which you initialized them.
以上都是。你用相同的地址初始化了几个指针,所以它们保持相同的地址,这和初始化它们的地址是一样的。
Perhaps more interestingly, x.a
is also guaranteed to point to itself (i.e., the first element in a struct is guaranteed to be at the very beginning of the struct, so a pointer to the struct, converted to the type of the first element, is guaranteed to point to that first element.
也许更有趣的是,x。a也被保证指向它自己(即。,确保结构体中的第一个元素位于结构体的最开始,因此,将指向结构体的指针转换为第一个元素的类型,可以保证指向第一个元素。
#1
8
This is standard C code.
这是标准的C代码。
This paragraph of the mighty Standard permits it (emphasis mine):
强有力的标准的这一段允许它(强调我的):
(C99, 6.2.1p7) "Structure, union, and enumeration tags have scope that begins just after the appearance of the tag in a type specifier that declares the tag. Each enumeration constant has scope that begins just after the appearance of its defining enumerator in an enumerator list. Any other identifier has scope that begins just after the completion of its declarator."
(C99 6.2.1p7)“结构、联合和枚举标记具有范围,范围从声明标记的类型说明符中的标记出现之后开始。每个枚举常数的作用域都在枚举数列表中定义枚举数出现后开始。任何其他标识符的作用域都是在声明符完成之后开始的。
For information, note that to illustrate the last sentence of 6.2.1p7, the book "The New C Standard" by Derek M. Jones uses an example similar to yours:
有关资料,请注意,德里克·琼斯(Derek M. Jones)的《新C标准》(the New C Standard)一书中,为了阐明6.1 .1p7的最后一句话,用了一个与你相似的例子:
struct T {struct T *m;} x = /* declarator complete here. */ {&x};
#2
8
Yes to all of the above. You have a couple of pointers that you're initializing with the same address, so they hold the same address, and that's the same as the address with which you initialized them.
以上都是。你用相同的地址初始化了几个指针,所以它们保持相同的地址,这和初始化它们的地址是一样的。
Perhaps more interestingly, x.a
is also guaranteed to point to itself (i.e., the first element in a struct is guaranteed to be at the very beginning of the struct, so a pointer to the struct, converted to the type of the first element, is guaranteed to point to that first element.
也许更有趣的是,x。a也被保证指向它自己(即。,确保结构体中的第一个元素位于结构体的最开始,因此,将指向结构体的指针转换为第一个元素的类型,可以保证指向第一个元素。