本文介绍了类型转换变量与其他的typedef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  typedef结构{
  unsigned char型一,
  unsigned char型B,
  unsigned char型Ç
} type_a;typedef结构{
 unsigned char型E,
 unsigned char型F [2]
} type_b;type_a样本;
sample.a = 1;
sample.b = 2;
sample.c文件= 3;

我如何强制转换它,并赋予新的价值,如:

 样品=(type_b)样品; //语法错误
sample.f [1] ='A';


解决方案

您不能施放一种数据类型到另一种不兼容的数据类型。然而,存储器是为你打开。您可以通过以下方式访问它:

  typedef结构
{
  unsigned char型一;
  unsigned char型B:
  unsigned char型℃;
} type_a;typedef结构
{
 unsigned char型ê;
 unsigned char型F [2];
} type_b;type_a样本;
sample.a = 1;
sample.b = 2;
sample.c文件= 3;type_b * sample_b =(type_b *)((无效*)及样品);

试用自己访问 sample_b-> e sample_b-方式>˚F键,看看会发生什么

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 can't cast one data type to another incompatible data type. However, the memory is open for you. 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.

这篇关于类型转换变量与其他的typedef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 22:47