使用指针数学访问结构元素 - 填充担心? [英] Accessing structure elements using pointer math - padding worries?

查看:47
本文介绍了使用指针数学访问结构元素 - 填充担心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在

的结构成员中使用指针算法吗?我应该担心结构填充吗?这在我的调试器中有效,但我想知道我是否在这里弯曲一些规则。


struct char_struct {

unsigned char a;

unsigned char b;

unsigned char c;

unsigned char d;

};


struct char_struct test = {50,75,100,225};


unsigned char * c;


c =& test;


如果(c [2] == 100)//这没关系?

printf(" c [2] is 100 QUOT); //这确实有效


c ++; //这没关系?

printf(" * c is 75"); //这确实有效

Can I use pointer arithmetic on the members of a structure in the
following way? Should I be worried about structure padding? This works
in my debugger but I wonder if I''m bending some rule here.

struct char_struct {
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
};

struct char_struct test = {50,75,100,225};

unsigned char *c;

c = &test;

if (c[2]==100) // this is okay?
printf("c[2] is 100"); // this does work

c++; // this is okay?
printf("*c is 75"); // this does work

推荐答案


qu ******** @ gmail.com 写道:

我可以对成员使用指针算法

中的结构如下?
Can I use pointer arithmetic on the members of a structure in the
following way?



是的,但不完全是你的意思。

Yes, but not quite as you mean.


我应该担心结构填充吗?
Should I be worried about structure padding?



是的。

Yes.


这在我的调试器中工作了

但是我想知道如果我在这里弯曲一些规则。


struct char_struct {

unsigned char a;

unsigned char b;

unsigned char c;

unsigned char d;

};


struct char_struct test = { 50,75,100,225};


unsigned char * c;


c =& test;


如果(c [2] == 100)//这没关系?
This works
in my debugger but I wonder if I''m bending some rule here.

struct char_struct {
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
};

struct char_struct test = {50,75,100,225};

unsigned char *c;

c = &test;

if (c[2]==100) // this is okay?



从你可以通过无符号

字符指针来解决任何问题,这是正常的,索引是少于

结构的大小。虽然它不一定能拿起结构的成员c


It''s OK in the sense that "you can address anything through an unsigned
character pointer", and the index is less that the size of the
structure. It doesn''t necessarily pick up member c of the structure
though.


printf(" c [2] is 100" ); //这确实有效
printf("c[2] is 100"); // this does work



巧合,取决于编译器/平台。

By coincidence, depending on the compiler/platform.


c ++; //这没关系?
c++; // this is okay?



是的,只要你留在结构或步骤之内就可以超越

吧。

Yes, as long as you stay within the structure or step to just beyond
it.


printf(" * c is 75"); //这确实有用
printf("*c is 75"); // this does work



再次,不保证是会员d。

Again, not guaranteed to be member d.


可以我在

的结构成员中使用指针算法?我应该担心结构填充吗?这在我的调试器中有效,但我想知道我是否在这里弯曲一些规则。


struct char_struct {

unsigned char a;

unsigned char b;

unsigned char c;

unsigned char d;

};


struct char_struct test = {50,75,100,225};


unsigned char * c;


c =& test;


如果(c [2] == 100)//这没关系?

printf(" c [2] is 100 QUOT); //这确实有效


c ++; //这没关系?

printf(" * c is 75"); //这确实有效
following way? Should I be worried about structure padding? This works
in my debugger but I wonder if I''m bending some rule here.

struct char_struct {
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
};

struct char_struct test = {50,75,100,225};

unsigned char *c;

c = &test;

if (c[2]==100) // this is okay?
printf("c[2] is 100"); // this does work

c++; // this is okay?
printf("*c is 75"); // this does work



不推荐这种访问方式。在编译器中没有添加任何

pad,因此您的测试程序可以正常工作。这可能不适用于这样的平台:

char'被填充到16或32位边界。


以下文章应该有所帮助:
http://www.eventhelix.com/RealtimeMa...ndOrdering。 htm


-

EventStudio系统设计器2.5 - http://www.EventHelix.com/EventStudio

基于序列图的实时和嵌入式系统设计工具

This type of access is not recommended. In compiler is not adding any
pads so your test program works. This may not work on a platform where
the char''s get padded to 16 or 32 bit boundaries.

The following article should help:
http://www.eventhelix.com/RealtimeMa...ndOrdering.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based Real-time and Embedded System Design Tool


>我可以对
>Can I use pointer arithmetic on the members of a structure in the

>中的结构成员使用指针算法吗?
>following way?



No.

No.


>我应该担心结构填充吗?
>Should I be worried about structure padding?



是的。

Yes.


>这在我的调试器中工作
但我想知道我是否我在这里弯曲一些规则。
>This works
in my debugger but I wonder if I''m bending some rule here.



正在吹嘘用C4考虑弯曲的smithereens?

Is blowing to smithereens with C4 considered "bending"?


> struct char_struct {

unsigned char a;

unsigned char b;

unsigned char c;

unsigned char d;
};
>struct char_struct {
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
};



如果您想将数组作为结构成员,请将数组用作

结构成员。

If you want an array as a structure member, USE an array as a
structure member.


> struct char_struct test = {50,75,100,225};

unsigned char * c;

c =& test;

如果(c [2] == 100)//这没关系?
>struct char_struct test = {50,75,100,225};

unsigned char *c;

c = &test;

if (c[2]==100) // this is okay?



No.

No.


printf(" c [2] is 100); //这确实有用
printf("c[2] is 100"); // this does work



在您的YOUR编译器版本上,也许只要你这么做
就不要加一个双结构的第五个成员。

On YOUR version of YOUR compiler and perhaps only as long as you
don''t add a double as a fifth member of the structure.


>
c ++; //这没关系?
>
c++; // this is okay?



是的。将单个变量视为一个元素的数组。你允许
计算一个超过最后一个元素的地址

但不能取消引用它。

Yes. Consider a single variable as an array of one element. You
are allowed to compute the address of "one past the last element"
but not dereference it.


> printf(" * c is 75"); //这确实有效
>printf("*c is 75"); // this does work



好​​了,打印一个常量字符串是允许的,但是如果你计划在增加它后取消引用c,那么
,不,那是不允许的。


Gordon L. Burditt

Ok, printing a constant string is allowed, but if you were planning
on dereferencing c after incrementing it, NO, that''s not allowed.

Gordon L. Burditt


这篇关于使用指针数学访问结构元素 - 填充担心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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