关于指向对象的指针和指向函数的指针的问题 [英] Questions about pointers to objects and pointers to functions

查看:59
本文介绍了关于指向对象的指针和指向函数的指针的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我有几个问题:


1)鉴于两个结构

struct A {

int x;

};





struct B {

struct A y;

int z;

};


是可以处理指向结构B类型的对象的指针。作为一个

指向结构A类型的对象的指针?

(我想有人问过这样的事情,但是

不幸的是我再也找不到这篇文章了。


2)当我现在有一个类型为
的函数指针时
void(* fpa)(struct A *,int);

和类型的函数指针

void(* fpb)(struct B *,int);


和相应的函数

void fa(struct A *,int);



void fb(struct B *,int );


可以分配一个指向fa的指针to fpb并通过fpb调用函数

,带有指向struct B类型的对象的指针。作为第一个

参数?


提前致谢

Marc Thrun

Hello,

I''ve got a few questions:

1) Given the two structs
struct A {
int x;
};

and

struct B {
struct A y;
int z;
};

is it ok to treat a "pointer to an object of type struct B" as a
"pointer to an object of type struct A"?
(I think someone asked something like this some time ago, but
unfortunately I can''t find the article anymore)

2) When I now have a function pointer of type
void (*fpa)(struct A *, int);
and a function pointer of type
void (*fpb)(struct B *, int);

and the corresponding functions
void fa(struct A*,int);
and
void fb(struct B*,int);

is it ok to assign a "pointer to fa" to fpb and call the function
through fpb with a "pointer to an object of type struct B" as the first
parameter?

Thanks in advance
Marc Thrun

推荐答案

Marc Thrun< Te ******** @ gmx.de>写道:

#您好,



#我有几个问题:



#1)给定两个结构

#struct A {

#int x;

#};



#和



#struct B {

#struct A y;

#int z;

#};



#可以治疗 ;指向struct B"类型的对象的指针as a

#"指向struct A类型对象的指针"?


给定

struct B sample

你是保守的

(结构A *)(及样本)==&(sample.y)

即指向a的指针struct也是指向

第一个字段的指针。

-

SM Ryan http://www.rawbw.com/~wyrmwif/

谁领导这些暴徒?
Marc Thrun <Te********@gmx.de> wrote:
# Hello,
#
# I''ve got a few questions:
#
# 1) Given the two structs
# struct A {
# int x;
# };
#
# and
#
# struct B {
# struct A y;
# int z;
# };
#
# is it ok to treat a "pointer to an object of type struct B" as a
# "pointer to an object of type struct A"?

Given
struct B sample
you are guarenteed
(struct A*)(&sample) == &(sample.y)
that is, a pointer to a struct is also a pointer to the
first field.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
Who''s leading this mob?


Marc Thrun< Te ******** @ gmx.de>写道:
Marc Thrun <Te********@gmx.de> writes:
你好,
我有几个问题:

1)鉴于两个结构
struct A {
int x;
};



struct B {
struct A y;
int z;
};

是否可以处理指向struct B类型的对象的指针。作为一个
指向结构A类型的对象的指针?(我想不久前有人问过类似的东西,但不幸的是我再也找不到这篇文章了)


这个问题可能意味着几件不同的事情。如果你要求b $ b转换(铸造)一个''struct B *''到''struct A *''

然后必须工作,例如


struct B * b = ...;

struct A * a;


a =(struct A *)b;

if(a ==& b-> y)/ * this''if''将永远采取* /


您可能会直接询问转换对象表示

,例如,使用


memcpy(& a,& b,sizeof a) ); / * 1 * /


a = *(结构A **)& b; / * 2 * /


要么/ * 1 * /或/ * 2 * /还应该产生一个可用的指针

通过''如果''测试上面。也就是说,最好的理解是标准中的语言应该理解为什么?
。有可能辩论这一点;使用的语言

在谈论这类事情时并不是十分明确。然而,作为一个实际问题,我们有理由期望这个

结果将在任何实际实现中保留。


2)当我现在有一个类型为
的函数指针void(* fpa)(struct A *,int);
和类型为
的函数指针void(* fpb)( struct B *,int);

和相应的函数
void fa(struct A *,int);

void fb(struct B *,int );

是否可以分配指向fa的指针到fpb并通过fpb用指向struct B类型的对象的指针调用函数
作为第一个
参数?
Hello,

I''ve got a few questions:

1) Given the two structs
struct A {
int x;
};

and

struct B {
struct A y;
int z;
};

is it ok to treat a "pointer to an object of type struct B" as a
"pointer to an object of type struct A"?
(I think someone asked something like this some time ago, but
unfortunately I can''t find the article anymore)
This question might mean a couple of different things. If you''re
asking about converting (casting) a ''struct B *'' to a ''struct A *''
then that has to work, eg

struct B *b = ...;
struct A *a;

a = (struct A*) b;
if( a == &b->y ) /* this ''if'' will always be taken */

You might be asking about converting the object representation
directly, eg, with one of

memcpy( &a, &b, sizeof a ); /* 1 */

a = * (struct A**) &b; /* 2 */

Either /*1*/ or /*2*/ should also result in a usable pointer that
passes the ''if'' test above. That is to say, to the best of my
understanding that is how the language in the Standard should be
understood. It''s possible to debate the point; the language used
in talking about such things is not completely clear cut. As a
practical matter, however, it''s reasonable to expect that this
result will hold in any actual implementation.

2) When I now have a function pointer of type
void (*fpa)(struct A *, int);
and a function pointer of type
void (*fpb)(struct B *, int);

and the corresponding functions
void fa(struct A*,int);
and
void fb(struct B*,int);

is it ok to assign a "pointer to fa" to fpb and call the function
through fpb with a "pointer to an object of type struct B" as the first
parameter?




技术上这样的通话是非法的,因为这两种类型不兼容。但是,即使它在技术上是非法的,它几乎肯定会在任何实际实施中起作用。



Technically such a call is illegal, since the two types are not
compatible. But, even though it''s technically illegal, it''s
almost certainly going to work in any actual implementation.


Tim Rentsch写道:
Tim Rentsch wrote:
Marc Thrun< Te ******** @ gmx.de>写道:

Marc Thrun <Te********@gmx.de> writes:

你好,
我有几个问题:

1)鉴于这两个问题结构
结构A {
int x;
};



结构B {
结构A y;
int z;
};

是否可以处理指向struct B类型的对象的指针。作为一个
指向结构A类型的对象的指针?(我想不久前有人问过类似的东西,但不幸的是我再也找不到这篇文章了)

这个问题可能意味着几件不同的事情。如果您要求将''struct B *''转换(转换)为''struct A *''
然后必须工作,例如
Hello,

I''ve got a few questions:

1) Given the two structs
struct A {
int x;
};

and

struct B {
struct A y;
int z;
};

is it ok to treat a "pointer to an object of type struct B" as a
"pointer to an object of type struct A"?
(I think someone asked something like this some time ago, but
unfortunately I can''t find the article anymore)

This question might mean a couple of different things. If you''re
asking about converting (casting) a ''struct B *'' to a ''struct A *''
then that has to work, eg



我承认它有点不清楚,对不起我不太好的英语

;-)。但你是对的,我的意思是铸造(我只是想知道为什么我没有

实际上是这个术语)。 struct B * b = ...;
struct A * a;

a =(struct A *)b;
if(a ==& b-> y)/ *这个''如果'将永远采取* /

您可能会直接询问转换对象表示
,例如,使用其中一个

memcpy(& a,& b,sizeof a); / * 1 * /

a = *(结构A **)& b; / * 2 * /

/ * 1 * /或/ * 2 * /也应该产生一个可用的指针,通过上面的if测试。也就是说,最好的理解是应该如何理解标准中的语言。有可能辩论这一点;在谈论这类事情时使用的语言并不完全明确。然而,作为一个实际问题,期望在任何实际实现中都能保持这个结果是合理的。


I admit that it''s quite a bit unclear, sorry for my not so good english
;-). But you are right, I meant casting (I just wonder why I did not
take this term actually). struct B *b = ...;
struct A *a;

a = (struct A*) b;
if( a == &b->y ) /* this ''if'' will always be taken */

You might be asking about converting the object representation
directly, eg, with one of

memcpy( &a, &b, sizeof a ); /* 1 */

a = * (struct A**) &b; /* 2 */

Either /*1*/ or /*2*/ should also result in a usable pointer that
passes the ''if'' test above. That is to say, to the best of my
understanding that is how the language in the Standard should be
understood. It''s possible to debate the point; the language used
in talking about such things is not completely clear cut. As a
practical matter, however, it''s reasonable to expect that this
result will hold in any actual implementation.

2)当我现在有类型为
的函数指针void(* fpa)(struct A *,int);
和类型为
的函数指针void(* fpb)(struct B *,int);

和相应的函数
void fa(struct A *,int);

void fb(struct B *,int);

可以分配一个指向fa的指针吗?到fpb并通过fpb用指向struct B类型的对象的指针调用函数
作为第一个
参数?
2) When I now have a function pointer of type
void (*fpa)(struct A *, int);
and a function pointer of type
void (*fpb)(struct B *, int);

and the corresponding functions
void fa(struct A*,int);
and
void fb(struct B*,int);

is it ok to assign a "pointer to fa" to fpb and call the function
through fpb with a "pointer to an object of type struct B" as the first
parameter?



技术上这样的电话是非法的,因为这两种类型不兼容。但是,即使它在技术上是非法的,它几乎肯定会在任何实际的实现中起作用。


Technically such a call is illegal, since the two types are not
compatible. But, even though it''s technically illegal, it''s
almost certainly going to work in any actual implementation.




我也这么认为,但不确定。假设我在两个函数中使用void *作为

的第一个参数,并将其转换为类型

" struct A *" /" struct B * "应该是有效的吗?


PS:我在哪里可以获得标准的副本(也许是草稿版本)?

下次可能会帮助我; - )。



I thought the same, but was not sure. Assuming I would use a void * for
the first parameter in both functions, and casting it to the type
"struct A*"/"struct B*" should be valid then?

PS: Where can I get a copy of the standard (maybe a draft version)?
Might help me next time ;-).


这篇关于关于指向对象的指针和指向函数的指针的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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