简体   繁体   English

使用另一个 typedef 类型转换变量

[英]Typecasting variable with another typedef

typedef struct {
  unsigned char a,
  unsigned char b, 
  unsigned char c
}type_a;

typedef struct {
 unsigned char e,
 unsigned char f[2]
}type_b;

type_a sample;
sample.a = 1;
sample.b = 2;
sample.c = 3;

How do I typecast it and assign new value like:我如何对其进行类型转换并分配新值,例如:

sample = (type_b)sample; // syntax error
sample.f[1] = 'a';

You should really try it out yourself.你真的应该自己尝试一下。

sample = (type_b)sample; /* Can't cast a structure to
                            another structure. */

sample.f[1] = 'a'; /* sample is still of type type_a,
                      and doesn't have an `f` field. */

No - C types are static, which means that sample will always remain of type type_a .否 - C 类型是 static,这意味着sample将始终保持类型type_a However, you can achieve what you want using unions:但是,您可以使用联合来实现您想要的:

union {
    type_a as_a;
    type_b as_b;
} sample;

sample.as_a.a = 1;
sample.as_a.b = 2;
sample.as_a.c = 3;

sample.as_b.f[1] = 'a';

Note that it is not usual to create an object that is a bare union type like this;请注意,通常不会创建像这样的裸union类型的 object; normally you would include the union within a struct , that includes a tag so that you know what type the object is at the present time:通常,您会将union包含在一个struct中,其中包含一个标签,以便您知道 object 目前是什么类型:

struct {
    enum { TYPE_A, TYPE_B } type;
    union {
        type_a as_a;
        type_b as_b;
    } data;
} sample;

/* sample is a TYPE_A right now */
sample.type = TYPE_A;
sample.data.as_a.a = 1;
sample.data.as_a.b = 2;
sample.data.as_a.c = 3;

/* sample is now a TYPE_B */
sample.type = TYPE_B;
sample.data.as_b.f[1] = 'a';

You can't cast one data type to another incompatible data type.您不能将一种数据类型转换为另一种不兼容的数据类型。 However, the memory is open for you.但是,memory 对您开放。 You can access it as follows:您可以按如下方式访问它:

typedef struct
{
  unsigned char a;
  unsigned char b; 
  unsigned char c;
}type_a;

typedef struct
{
 unsigned char e;
 unsigned char f[2];
}type_b;

type_a sample;
sample.a = 1;
sample.b = 2;
sample.c = 3;

type_b *sample_b = (type_b *) ((void*) &sample);

Try out yourself accessing sample_b->e and sample_b->f and see what happens.尝试自己访问sample_b->esample_b->f看看会发生什么。

No. You can do it by casting pointers: value_b = *((value_b*)&value_a) or by creating union of those two types.不,您可以通过转换指针来做到这一点: value_b = *((value_b*)&value_a)或通过创建这两种类型的联合。

However you do it, be careful.不管你怎么做,都要小心。 Structures can have different data alignment and you may get unexpected results.结构可以有不同的数据 alignment,你可能会得到意想不到的结果。

yes you can copy the value of the type_a into type_b by trying something like是的,您可以通过尝试类似的方法将 type_a 的值复制到 type_b 中

type_b sample_b =*((type_b*)&sample); type_b sample_b =*((type_b*)&sample);

or或者

memcpy(&sample_b,&sample,sizeof(type_a)); memcpy(&sample_b,&sample,sizeof(type_a));

Typecasting is nothing but converting an expression of one type to another one.类型转换只不过是将一种类型的表达式转换为另一种类型。 But you seem to be trying to convert the type itself, which is fixed at compile time(variable declaration)但是您似乎正在尝试转换类型本身,这是在编译时固定的(变量声明)

Its not clear the idea behind trying something like this.目前尚不清楚尝试这样的事情背后的想法。 If you can make it more clear, people would be able to give more insights如果你能说得更清楚,人们就能提供更多的见解

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM