为什么C允许结构成员像数组一样被寻址? [英] How come C allow structure members to be addressed like an array ?

查看:54
本文介绍了为什么C允许结构成员像数组一样被寻址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< stdio.h>


typedef struct

{

double x,y,z; < br $>
}向量;


int main(无效)

{

int i;

vector v;

double * cord;


vx = 10;

vy = 1;

vz = 2;


cord =& vx;


for(i = 0; i< 3 ; i ++)

{

printf("%f\ n",cord [i]);

}

返回0;

}


这里是我得到的输出:

10.000000

1.000000

2.000000

这与v相同

i不怎么样因为我只是尝试了一些随意的想法,但是真的很棒的东西。帮我把我的一些代码减少到几乎相当于它的大小的1/3美元。

解决方案

6月14日,5日:37 * pm,pereges< Brol ... @ gmail.comwrote:


#include< stdio.h>


typedef struct

{

* double x,y,z;


} vector;


int main(无效)

{

* int i;

* vector v;

* double * cord;


* vx = 10;

* vy = 1;

* vz = 2 ;


* cord =& vx;


* for(i = 0; i< 3; i ++)

* {

* printf("%f\ n",cord [i]);

*}

返回0;


}


这里是我得到的输出:

10.000000

1.000000

2.000000

这与v相同

i不怎么样因为我只是尝试了一些随意的想法,但

很棒东西真的。帮我把我的一些代码减少到差不多大小的1/3美元。



这不是便携式但是在低级别上没有区别

包含三个双打和一个三个数组的结构之间在这种情况下,
加倍。


你不能假设这一点,因为一些实现可能会为结构中的字节对齐引入焊盘

。 />

以下文章应该有所帮助:

http://www.eventhelix.com/RealTimeMa...ranslation.htm
http://www.eventhelix.com/RealTimeMa...anslation2.htm
http://www.eventhelix.com/RealTimeMa...anslation3.htm

-

EventStudio 4.0 - http://www.Eventhelix.com/Eventstudio/

序列图基于系统的工程工具


文章< a0 ************************** ********@34g2000hsf.g ooglegroups.com>,

EventHelix.com< ev ******** @ gmail.comwrote:


>这不是可移植的,但是在低级别,在这种情况下,包含三个双精度的结构和三个双精度数组之间没有区别。



结构中的字段以实现定义的方式

对齐,适合[他们的类型]。明确的含义是

填充是该类型的属性(而不是每个单独的

实例),因此填充
是不合理的。
结构中相同类型的连续成员与数组中的

不同 - 也就是说,

字段之间应该没有填充相同类型的。


任何人都可以提出一个理由(除了乖僻)为什么

实现不会这样做?


- 理查德

-

在紧接着数字9的两个字符的选择中,

考虑应由图形10和11替换为

,以便在纯正货币区域采用该代码。 (X3.4-1963)


可以使用下面的矢量迭代器函数来解决这个问题

但我不确定是否解决方案是通用的:


#include< stdio.h>


typedef struct

{

double x,y,z;


} vector;


double vector_iterator(vector * a,int i)

{

double ret;

if(i == 0)

ret = a-> x;

if(i == 1)

ret = a-> y;

if(i == 2)

ret = a-> z;

返回ret;

}


int main(void)

{

int i;

vector v;

double coord;


vx = 10;

vy = 1;

vz = 2;


for(i = 0; i< ; 3; i ++)

{

coord = vector_iterator(& v,i);

printf("%f\\\
" ;,coord);

}

返回0;


}


#include <stdio.h>

typedef struct
{
double x, y, z;
}vector;

int main(void)
{
int i;
vector v;
double *cord;

v.x = 10;
v.y = 1;
v.z = 2;

cord = &v.x;

for(i = 0; i < 3; i++)
{
printf("%f\n", cord[i]);
}
return 0;
}

here''s the output i get:
10.000000
1.000000
2.000000

which is the same as v

i don''t how it happens as i was just trying some random ideas but
great stuff really. helped me to reduce some of my code to almost
1/3rd its size.

解决方案

On Jun 14, 5:37*pm, pereges <Brol...@gmail.comwrote:

#include <stdio.h>

typedef struct
{
* double x, y, z;

}vector;

int main(void)
{
* int i;
* vector v;
* double *cord;

* v.x = 10;
* v.y = 1;
* v.z = 2;

*cord = &v.x;

*for(i = 0; i < 3; i++)
*{
* printf("%f\n", cord[i]);
*}
return 0;

}

here''s the output i get:
10.000000
1.000000
2.000000

which is the same as v

i don''t how it happens as i was just trying some random ideas but
great stuff really. helped me to reduce some of my code to almost
1/3rd its size.

This is not portable but at the low level there is no difference
between a structure containing three doubles and an array of three
doubles in this case.

You cannot assume this, as some implementation might introduce pads
for byte alignment in structures.

The following articles should help:

http://www.eventhelix.com/RealTimeMa...ranslation.htm
http://www.eventhelix.com/RealTimeMa...anslation2.htm
http://www.eventhelix.com/RealTimeMa...anslation3.htm

--
EventStudio 4.0 - http://www.Eventhelix.com/Eventstudio/
Sequence diagram based systems engineering tool


In article <a0**********************************@34g2000hsf.g ooglegroups.com>,
EventHelix.com <ev********@gmail.comwrote:

>This is not portable but at the low level there is no difference
between a structure containing three doubles and an array of three
doubles in this case.

Fields in a struct are aligned in "an implementation-defined manner
appropriate to [their] type". The clear implication is that the
padding is a property of the type (rather than each individual
instance) and so it would be unreasonable for the padding of
successive members of the same type in a struct to be different from
that in an array - which is to say, there should be no padding between
fields of the same type.

Can anyone come up with a reason (other than perverseness) why an
implementation would not do this?

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)


one can solve this problem using a vector iterator function like below
but i am not sure if the solution is generic :

#include <stdio.h>

typedef struct
{
double x, y, z;

}vector;

double vector_iterator(vector *a, int i)
{
double ret;
if(i == 0)
ret = a->x;
if(i == 1)
ret = a->y;
if(i == 2)
ret = a->z;
return ret;
}

int main(void)
{
int i;
vector v;
double coord;

v.x = 10;
v.y = 1;
v.z = 2;

for(i = 0; i < 3; i++)
{
coord = vector_iterator(&v, i);
printf("%f\n", coord);
}
return 0;

}


这篇关于为什么C允许结构成员像数组一样被寻址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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