一个元素数组作为struct typedef的名称 [英] one element array as name of struct typedef

查看:82
本文介绍了一个元素数组作为struct typedef的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在讨论GNU

MP库如何实现其mpz_t类型时碰到了以下内容。


typedef struct {

int len;

char * buf;

} foo [1];


这是一项有趣的技巧。我不熟悉将typedef的

名称声明为单元素数组。这是C

标准的一部分吗?它是便携式的吗?这种技术有名吗?

I ran into something like the following while looking into how the GNU
MP library implements its mpz_t type.

typedef struct {
int len;
char *buf;
} foo[1];

This is an interesting technique. I''m unfamiliar with declaring the
name of a typedef as a one element array. Is this part of the C
standard? Is it portable? Does this technique have a name?

推荐答案

Ron Peterson(012ED25E)写道:
Ron Peterson (012ED25E) wrote:

我在调查GNU

MP库如何实现其mpz_t类型时碰到了以下内容。


typedef struct {

int len;

char * buf;

} foo [1];


这是一个有趣的技术。我不熟悉将typedef的

名称声明为单元素数组。这是C

标准的一部分吗?它是便携式的吗?这种技术有名字吗?
I ran into something like the following while looking into how the GNU
MP library implements its mpz_t type.

typedef struct {
int len;
char *buf;
} foo[1];

This is an interesting technique. I''m unfamiliar with declaring the
name of a typedef as a one element array. Is this part of the C
standard? Is it portable? Does this technique have a name?



嗯....我想你错了这意味着什么。


我的阅读(可能容易不正确的是它使得foo a

typedef,用于单个维度,单个元素,此类结构的数组。


我正试图找出这种技术的价值会是。


Hmmm.... I think you''re mistaken in what this means.

My reading (which could easily be incorrect) is that it makes "foo" a
typedef for a single dimension, single element, array of such structures.

I''m trying to work out what the value of such a technique would be.


在文章< fb ********** @ aioe.org>,

Mark Bluemel< ; ma ********** @ pobox.comwrote:
In article <fb**********@aioe.org>,
Mark Bluemel <ma**********@pobox.comwrote:

> typedef struct {
int len;
char * buf;
} foo [1];
>typedef struct {
int len;
char *buf;
} foo[1];


>我正在试图找出这种技术的价值。
>I''m trying to work out what the value of such a technique would be.



我的猜测是,通过引用自动传递(

效果)对象是一个巧妙的技巧。如果它是


typedef struct {...} foo;


你将不得不在整个地方使用& foo,并且它已经是


typedef struct {...} * foo;


你必须分配数据以及声明一个变量。


- Richard

-

应考虑多达32个的需要字符
某些字母表中的
" - X3.4,1963。

My guess is that it''s a neat trick for having objects that are (in
effect) automatically passed by reference. If it had been

typedef struct {...} foo;

you would have to use &foo all over the place, and it it had been

typedef struct {...} *foo;

you would have to allocate the data as well as declaring a variable.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.



我在研究GNU如何时遇到了类似的问题>
MP库实现其mpz_t类型。


typedef struct {

int len;

char * buf;

} foo [1];


这是一项有趣的技巧。我不熟悉将typedef的

名称声明为单元素数组。这是C

标准的一部分吗?它是便携式的吗?这种技术有名字吗?
I ran into something like the following while looking into how the GNU
MP library implements its mpz_t type.

typedef struct {
int len;
char *buf;
} foo[1];

This is an interesting technique. I''m unfamiliar with declaring the
name of a typedef as a one element array. Is this part of the C
standard? Is it portable? Does this technique have a name?



typeof(foo)== 1元素给定结构的数组


因此可以声明实际数组或定义为:


foo fooarray; / *注意:不需要尺寸信息* /


这个技术在指向数组的指针时很有用(我指的是整个数组,而不是

只是指向元数据类型)是必需的。

阅读C FAQ。 Ther是关于指针和数组的整整一章。


typeof(foo) == array of the given struct of 1 element

so the actual array may be declared or defined as:

foo fooarray; /* Note: No dimension info required */

the technique is useful when a pointer to an array (i mean entire array, not
just pointer to element data type) is required.
read C FAQ. Ther is a entire chapter devoted to pointers and array.



这篇关于一个元素数组作为struct typedef的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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