指针之间的差异 [英] Differences between pointers

查看:48
本文介绍了指针之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


只是澄清了关于C-Standard'

指针解释的规范。

我知道''char *''和''void *''是等价的,并且''int *''可能与''char *'的表示形式不同
'(但在现代编译器/机器上通常没有

的差异)。


因此,如果我定义一个函数,例如(通常用于回调)


void foo(void * data);


然后我传递的是一个结构的东西


struct testing {

int a;

char b;

};


int main(void)

{

struct testing x;

xa = 1;

xb = 2;


foo((void *)& x);

返回0;

}


这个概念有效吗?如果没有,那么实现

便携式结果的有效方法是什么?


最好的问候,

Jason。

Hello,

Just a clarification of the specification about the C-Standard''s
interpretation of pointers.

I know that ''char *'' and ''void *'' are equivalent and that ''int *'' may be
different in representation to ''char *'' (but most often there is no
difference on modern compilers/machines).

So if I define a function such as (e.g. often for callbacks)

void foo(void *data);

Then if I pass something that is a structure

struct testing {
int a;
char b;
};

int main(void)
{
struct testing x;
x.a = 1;
x.b = 2;

foo((void *)&x);
return 0;
}

is this concept valid? If not, what would be a valid way to achieve
portable results?

Best Regards,
Jason.

推荐答案

2005年4月13日星期三06:11:02 + 0200,Jason Curl

< j _ ***** ***@motorola.com>在comp.lang.c中写道:
On Wed, 13 Apr 2005 06:11:02 +0200, Jason Curl
<j_********@motorola.com> wrote in comp.lang.c:
你好,

只是澄清了关于C-Standard的解释的规范指针。

我知道''char *''和''void *''是等价的,''int *''在表示''char *时可能有所不同''(但最常见的是现代编译器/机器没有区别。)

所以,如果我定义一个函数,例如(通常用于回调)

> void foo(void * data);

然后,如果我传递了一个结构的东西

struct testing {
int a;
char b ;
};

int main(void)
{struct struct x;
xa = 1;
xb = 2;

foo((void *)& x);


演员阵容是不必要的。 & operator取其

操作数的地址,产生一个指向type的指针值,在这种情况下是指向

struct testing的指针。在C中,不需要强制转换将指针

转换为void和指向任何其他对象类型的指针,而不是任何一个方向。


所以重写为:


foo(& x);

返回0;
}
这个概念有效吗?如果没有,那么实现便携式结果的有效方法是什么?

最好的问候,Jason。
Hello,

Just a clarification of the specification about the C-Standard''s
interpretation of pointers.

I know that ''char *'' and ''void *'' are equivalent and that ''int *'' may be
different in representation to ''char *'' (but most often there is no
difference on modern compilers/machines).

So if I define a function such as (e.g. often for callbacks)

void foo(void *data);

Then if I pass something that is a structure

struct testing {
int a;
char b;
};

int main(void)
{
struct testing x;
x.a = 1;
x.b = 2;

foo((void *)&x);
The cast is unnecessary. The & operator takes the address of its
operand yielding a value of pointer to type, in this case pointer to
struct testing. In C, no cast is required to convert between pointer
to void and pointer to any other object type, not in either direction.

So rewrite this as:

foo(&x);
return 0;
}

is this concept valid? If not, what would be a valid way to achieve
portable results?

Best Regards,
Jason.




就你所写的代码而言,这个概念是完全有效和便携的。实际上使用指针是另一回事。它必须是

转换为指向对象类型的指针才能用于实际读取或写入值



可以转换为唯一的可移植类型是指向struct

测试的指针,或指向int的指针,在这种情况下它将指向''a''

成员测试结构。


-

Jack Klein

主页: http://JK-Technology.Com



comp.lang的常见问题解答。 c http://www.eskimo.com/~scs /C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib。 andrew.cmu.edu/~a ... FAQ-acllc.html



The concept is perfectly valid and portable as far as the code you
wrote. Actually using the pointer is another matter. It must be
converted to a pointer to object type before it can be used to
actually read or write values.

The only portable types it may be converted to are pointer to struct
testing, or pointer to int, in which case it will point to the ''a''
member of the testing structure.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


Jason Curl写道:
Jason Curl wrote:
你好,
<只是澄清了关于C-Standard对指针的解释的规范。

我知道'char *''和''void *''是相当于''int *''可能与''char *''的表示形式不同(但最常见的是现代编译器/机器上没有区别)。

void foo(void * data);

然后如果我传递了一个结构的东西

struct testing {
int a;
char b;
};

int main(void)
{struct struct x;
xa = 1;
xb = 2;

foo((void *)& x);
返回0;
}

是这个概念有效吗?如果没有,那么实现便携式结果的有效方法是什么?

最诚挚的问候,Jason。
Hello,

Just a clarification of the specification about the C-Standard''s
interpretation of pointers.

I know that ''char *'' and ''void *'' are equivalent and that ''int *'' may be
different in representation to ''char *'' (but most often there is no
difference on modern compilers/machines).

So if I define a function such as (e.g. often for callbacks)

void foo(void *data);

Then if I pass something that is a structure

struct testing {
int a;
char b;
};

int main(void)
{
struct testing x;
x.a = 1;
x.b = 2;

foo((void *)&x);
return 0;
}

is this concept valid? If not, what would be a valid way to achieve
portable results?

Best Regards,
Jason.



为什么要'你这样编写你的代码吗?

void foo(struct testing * data)????


-

当你感到孤独时,你开始想念我。

当我想念你时,我陷入了孤独。


Why don''t you write your code like this ?
void foo(struct testing *data)????

--
Just when you felt lonely, you began to miss me.
When I was missing you, I falled into lonely.


Jack Klein< ; JA ******* @ spamcop.net>写道:
Jack Klein <ja*******@spamcop.net> wrote:
2005年4月13日星期三06:11:02 + 0200,Jason Curl
< j _ ******** @ motorola.com>在comp.lang.c中写道:
On Wed, 13 Apr 2005 06:11:02 +0200, Jason Curl
<j_********@motorola.com> wrote in comp.lang.c:
我知道''char *''和''void *''相当于


嗯......差不多。他们需要具有相同的表示和

对齐,但只有void *可以在没有强制转换的情况下分配给其他对象

指针。

所以,如果我定义一个函数,例如(例如经常用于回调)

void foo(void * data);

然后,如果我传递的是结构<结构测试{
int a;
char b;
};

int main(void)
{ xa = 1;
xb = 2;

foo((void *)& x);
演员表是不必要的。
I know that ''char *'' and ''void *'' are equivalent
Well... almost. They''re required to have identical representation and
alignment, but only void * can be assigned to and from other object
pointers without casting.
So if I define a function such as (e.g. often for callbacks)

void foo(void *data);

Then if I pass something that is a structure

struct testing {
int a;
char b;
};

int main(void)
{
struct testing x;
x.a = 1;
x.b = 2;

foo((void *)&x);
The cast is unnecessary.



就你编写的代码而言,这个概念是完全有效和可移植的。实际上使用指针是另一回事。必须先将它转换为指向对象类型的指针才能用于实际读取或写入值。

它可以转换为唯一的可移植类型是指针struct
测试,或指向int的指针,在这种情况下,它将指向测试结构的''a''
成员。


The concept is perfectly valid and portable as far as the code you
wrote. Actually using the pointer is another matter. It must be
converted to a pointer to object type before it can be used to
actually read or write values.

The only portable types it may be converted to are pointer to struct
testing, or pointer to int, in which case it will point to the ''a''
member of the testing structure.




和指向unsigned char的指针,在这种情况下它可以用来检查结构的内存表示形式(这是实现 -

特定的,并且可能(可能)将)包括填充字节)。


Richard



And a pointer to unsigned char, in which case it may be used to inspect
the memory representation of the struct (which is implementation-
specific, and may (probably will) include padding bytes).

Richard


这篇关于指针之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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