[英]C99 pointer to compound literal array of pointers
NOTE: I am actively fiddling with this over on Ideone . 注意:我正在Ideone上对此事积极地摆弄。
I have a (self-referential) structure: 我有一个(自我引用)结构:
typedef struct T_Function T_Function;
struct T_Function
{
T_Function * (* inhibits)[]; // pointer to array of pointers to this structure
};
and would like to use compound literals as the target of the inhibits
pointer. 并希望使用复合文字作为
inhibits
指针的目标。 Something along the lines of 遵循以下原则
T_Function a, b, c;
a.inhibits = & (<type>) {&b, &c};
This could be done as follows but I am looking to understand the type specification so I can use compound literals. 可以按照以下步骤进行操作,但是我希望了解类型规范,以便可以使用复合文字。
T_Function a, b, c;
T_Function * ai[] = {&b, &c};
a.inhibits = &ai;
What is the appropriate type specification to replace <type>
above? 什么是替代上面的
<type>
的适当类型规范?
a.inhibits
is a pointer to some subsequent memory locations forming the pointer array. a.inhibits
是指向形成指针数组的某些后续存储位置的指针。 If you want to assign something to a.inhibits
, you need an array of pointers somewhere whose location to store in a.inhibits
. 如果要为
a.inhibits
分配某些a.inhibits
,则需要在a.inhibits
存储其位置的指针数组。 This is why there is no syntax like &(...){&b, &c}
: it can't be right regardless of what you are using for ...
because there is no actual array {&b, &c}
in the memory. 这就是为什么没有语法喜欢
&(...){&b, &c}
它不可能是正确的,无论你正在使用...
因为没有实际的数组{&b, &c}
在存储。
In your second example, you are allocating an array of T_Function *
pointers on the stack. 在第二个示例中,您正在堆栈上分配
T_Function *
指针数组。 This is a common programming error: a.inhibits
will point to a location on the stack, and as soon as the current stack frame is left, the contents of a.inhibits
will be undefined. 这是一个常见的编程错误:
a.inhibits
将指向堆栈上的某个位置,并且当当前堆栈帧离开时, a.inhibits
的内容将是不确定的。 Still worse, writing to a.inhibits
will cause undefined behavior. 更糟糕的是, 写入
a.inhibits
将导致未定义的行为。
You'll probably want to use a data structure of some kind, but to answer your question, the simplest solution would be to allocate the array on the heap: 您可能想要使用某种数据结构,但是要回答您的问题,最简单的解决方案是在堆上分配数组:
#include <stdlib.h>
typedef struct T_Function T_Function;
struct T_Function
{
T_Function **inhibits; /* no [] */
};
… …
T_Function a, b, c;
T_Function **ai = calloc(2, sizeof(T_Function *));
ai[0] = &b;
ai[1] = &c;
a.inhibits = ai;
Just make sure you free the memory once you don't need it any more. 只要确保您不再需要内存就可以释放它。
<type>
can be T_Function *[]
. <type>
可以是T_Function *[]
。
typedef struct T_Function T_Function;
typedef T_Function * T_Inhibits[];
struct T_Function
{
T_Inhibits * inhibitsTD; // pointer to array of pointers to this structure
T_Function * (* inhibits)[]; // pointer to array of pointers to this structure
};
<snip>
T_Function x;
// direct
x.inhibitsTD = &(T_Function *[]) {&x};
x.inhibits = &(T_Function *[]) {&x};
// via typedef
x.inhibitsTD = &(T_Inhibits) {&x};
x.inhibits = &(T_Inhibits) {&x};
Do note that as @user3426575 pointed out, there are storage duration hazards with this. 请注意,正如@ user3426575所指出的那样,这样做存在存储持续时间的危害。 Being a compound literal does not inherently imply static duration.
作为复合文字并不必然意味着静态持续时间。
The value of the compound literal is that of an unnamed object initialized by the initializer list.
复合文字的值是由初始化程序列表初始化的未命名对象的值。 If the compound literal occurs outside the body of a function, the object has static storage duration;
如果复合文字出现在函数主体之外,则对象具有静态存储持续时间; otherwise, it has automatic storage duration associated with the enclosing block.
否则,它具有与封闭块关联的自动存储时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.