在不同类型的结构中键入信息 [英] Type information in structs of different types

查看:65
本文介绍了在不同类型的结构中键入信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否定义明确且便于执行以下操作:


typedef struct

{

int type;

char c;

} S1;


typedef struct

{

int type;

float f;

} S2;


void f(void * p)

{

S1 * p1 =(S1 *)p;

S2 * p2 =(S2 *)p;


if(p1-> type == 1)

{

printf("%c\ n",p1-> c); < br $> b $ b}

其他

{

printf("%f\ n",p2-> f) );

}

}


int main(无效)

{

S1 s1;

s1.type = 1;

s1.c =''a'';


S2 s2;

s2.type = 2;

s2.f = 42.0;


f(& s1);

f(& s2);

}





如果是这样的话,是否有任何约束(结构的共享字段

必须在开始等)?


如果没有,是否有任何便携式方法可以做类似的事情?


/ David

Is it well-defined and portable to do something like:

typedef struct
{
int type;
char c;
} S1;

typedef struct
{
int type;
float f;
} S2;

void f(void* p)
{
S1* p1 = (S1*) p;
S2* p2 = (S2*) p;

if (p1->type == 1)
{
printf("%c\n", p1->c);
}
else
{
printf("%f\n", p2->f);
}
}

int main(void)
{
S1 s1;
s1.type = 1;
s1.c = ''a'';

S2 s2;
s2.type = 2;
s2.f = 42.0;

f(&s1);
f(&s2);
}

?

If so, are there any constraints (the "shared" fields of the structs
have to be at the beginning etc.)?

If not, is there any portable way to do something similar?

/David

推荐答案

2006年3月14日星期二13:00, pi ************ @ gmail.com 认为(

< 11 *********** **********@i40g2000cwc.googlegroups。 com>):
On Tuesday 14 March 2006 13:00, pi************@gmail.com opined (in
<11*********************@i40g2000cwc.googlegroups. com>):
它是否定义明确且可移植以执行以下操作:


我没有看到任何错误,但也见下面......

typedef struct
{
int type;
char c;
} S1;

typedef struct
{
int type;
float f;
} S2;

void f(void * p)
{
S1 * p1 =(S1 *)p;
S2 * p2 =(S2 *)p;

if(p1-> type == 1)
{
printf("%c\ n",p1-> c);
}

{
printf("%f) \ n",p2-> f);
}


这还不够精确。对于任何不是
的类型,它都会假设S2。这可能是这类技巧的最大问题。你需要非常小心:


a)总是正确地初始化类型字段

b)满足a)不是案件

}

int main(无效)
{s / s> S1 s1;
s1.type = 1;
s1.c =''a'';

S2 s2;
s2.type = 2;
s2.f = 42.0;

f (& s1);
f(& s2);
}


如果是的话,是否有任何限制(共享结构的领域
必须在开头等等。

如果没有,是否有任何便携式方法可以做类似的事情?

/大卫
Is it well-defined and portable to do something like:
I don''t see anything wrong with it, but see below as well...
typedef struct
{
int type;
char c;
} S1;

typedef struct
{
int type;
float f;
} S2;

void f(void* p)
{
S1* p1 = (S1*) p;
S2* p2 = (S2*) p;

if (p1->type == 1)
{
printf("%c\n", p1->c);
}
else
{
printf("%f\n", p2->f);
}
This is not precise enough. It''ll assume `S2` for any `type` that is not
1. This is probably the biggest issue with tricks like these. You
should take great care to:

a) always initialise the type field properly
b) cater for when a) is not the case
}

int main(void)
{
S1 s1;
s1.type = 1;
s1.c = ''a'';

S2 s2;
s2.type = 2;
s2.f = 42.0;

f(&s1);
f(&s2);
}

?

If so, are there any constraints (the "shared" fields of the structs
have to be at the beginning etc.)?

If not, is there any portable way to do something similar?

/David




-

BR,Vladimir


Ginsburg的定律:

在您在鞋店脱鞋的确切时刻,您的袜子会弹出你的袜子,看看发生了什么。



--
BR, Vladimir

Ginsburg''s Law:
At the precise moment you take off your shoe in a shoe store, your
big toe will pop out of your sock to see what''s going on.


pi ******** ****@gmail.com w死记硬背:
它是否定义明确且便于执行以下操作:


差不多。保证如果结构一起出现在一个联合中

那么任何常见的*初始*字段序列将被布置为相同的

并且你可以使用任何一个结构类型来访问它们。在实践中,它几乎所有没有联合的系统都可以使用


typedef struct
{
int type;
char c ;
} S1;

typedef struct
{
int type;
float f;
} S2;


union U12 {

s1:S1;

s2:S2;

};

void f(void * p)
{* / S1 * p1 =(S1 *)p;
S2 * p2 =(S2 *)p;

如果(p1-> type == 1)
{
printf("%c\ n,p1-> c);
}
其他
{
printf("%f\ n",p2-> f);
}
}

(无效)
{/ s> S1 s1;
s1.type = 1;
s1.c =''a'';

S2 s2;
s2.type = 2;
s2.f = 42.0;

f(& s1);
f(& s2);
如果是这样的话,是否有任何约束(结构的共享字段必须在开头等等。)

如果没有,是否有任何便携式方法可以做类似的事情?
Is it well-defined and portable to do something like:
Almost. It is guaranteed that if the structs appear together in a union
then any common *initial* sequence of fields will be laid out the same
and you can use any of the struct types to access them. In practice, it
will work on almost all systems without the union.
typedef struct
{
int type;
char c;
} S1;

typedef struct
{
int type;
float f;
} S2;
union U12 {
s1: S1;
s2: S2;
};
void f(void* p)
{
S1* p1 = (S1*) p;
S2* p2 = (S2*) p;

if (p1->type == 1)
{
printf("%c\n", p1->c);
}
else
{
printf("%f\n", p2->f);
}
}

int main(void)
{
S1 s1;
s1.type = 1;
s1.c = ''a'';

S2 s2;
s2.type = 2;
s2.f = 42.0;

f(&s1);
f(&s2);
}

?

If so, are there any constraints (the "shared" fields of the structs
have to be at the beginning etc.)?

If not, is there any portable way to do something similar?




这是一种标准技术,但要注意
在maxi的结构之间具有联合妈妈的便携性。虽然如果有任何系统在没有

联盟的情况下失败,我会感到惊讶。

-

Flash Gordon,生活在有趣的时代。

网站 - http://home.flash-gordon.me.uk/

comp.lang.c发布指南和介绍:
http://clc-wiki.net/wiki/Intro_to_clc


Le 14-03-2006, pi ************ @ gmail.com < pi ************ @ gmail.com> écrit*:
Le 14-03-2006, pi************@gmail.com <pi************@gmail.com> a écrit*:
它是否定义明确且可移植以执行以下操作:

typedef struct
{
int type;
char c;
} S1;

typedef struct
{
int type;
float f;
} S2;

void f(void * p)
{* S1 * p1 =(S1 *)p;
S2 * p2 =(S2 *)p;

if(p1-> type == 1)
{
printf("%c\ n",p1-> c);
}
else
{
printf("%f\ n",p2-> f);
}
}

int main(void)
{s / s> s1.type = 1;
s1.c =''a'';

S2 s2;
s2.type = 2;
s2.f = 42.0;

f(& s1);
f(& s2);
}

Is it well-defined and portable to do something like:

typedef struct
{
int type;
char c;
} S1;

typedef struct
{
int type;
float f;
} S2;

void f(void* p)
{
S1* p1 = (S1*) p;
S2* p2 = (S2*) p;

if (p1->type == 1)
{
printf("%c\n", p1->c);
}
else
{
printf("%f\n", p2->f);
}
}

int main(void)
{
S1 s1;
s1.type = 1;
s1.c = ''a'';

S2 s2;
s2.type = 2;
s2.f = 42.0;

f(&s1);
f(&s2);
}

?




不,不是。

见6.5.2.3/5和6.5.2.3/8详情。


Marc Boyer



No, it is not.
See 6.5.2.3/5 and 6.5.2.3/8 for details.

Marc Boyer


这篇关于在不同类型的结构中键入信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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