TCharInfoEx=record
  case Byte of
   0:(UnicodeChar:WCHAR;
      UAttributes:Word);
   1:(AsciiChar:CHAR;
      AAttributes:Word);
 end; TScreen=array[0..24,0..79] of TCharInfoEx;我想把上面所定义的结构用c语言写一遍,对TCharInfoEx 还是有点搞不清楚,
请教各位!!
谢谢!!struct TCharInfoExT 
{
union
{
WCHAR UnicodeChar;
WORD UAttributes;
}0;
union
{
CHAR AsciiChar;
WORD AAttributes;
}1;
};
typedef TCharInfoExT TScreen[25][80];这样写也是有问题,请教!!!

解决方案 »

  1.   

    楼主,你用 Delphi 定义的结构体本身就是有问题啊,
    明显 0 元素和 1 元素的字节长度不同么,怎么能行的通呢?TCharInfoEx=record
      case Byte of
       0:(UnicodeChar:WCHAR;
          UAttributes:Word);
       1:(AsciiChar1:CHAR;
          AsciiChar2:CHAR;
          AAttributes:Word);
     end;struct TCharInfoExT 
    {
    union
    {
    WCHAR UnicodeChar;
    WORD UAttributes;
    }0;
    union
    {
    CHAR AsciiChar1;
                      CHAR AsciiChar2;
    WORD AAttributes;
    }1;
    };//这样才对。
      

  2.   

    晕~不好意思,总也不写 C 了,
    你这个语法也有问题:
    照这么写:
    struct TCharInfoExT 
    {
    union
    {
                 struct
                 {
    WCHAR UnicodeChar;
    WORD UAttributes;
                 };
                 struct
                 {
    CHAR AsciiChar1;
                      CHAR AsciiChar2;
    WORD AAttributes;             };
    }
    };
      

  3.   

    cnmaxu(Max)
    这个定义好像可以了但是为什么在赋值的时候出错呢? TConsoleScreen LOldScreen;
    TConsoleScreen LNewScreen;
    TConsoleScreen LTmpScreen; LOldScreen = LNewScreen;最后的这句赋值语句报错,
    error C2106: '=' : left operand must be l-value;
      

  4.   

    it's should be like this:
    typedef struct tagTCharInfoExT 
    {
    union
    {
    struct
    {
    WCHAR UnicodeChar;
    WORD UAttributes;
    };
    struct
    {
    CHAR AsciiChar;
    WORD AAttributes;
    };
    };
    } TCharInfoExT;//
    TCharInfoEx=record
      case Byte of
       0:(UnicodeChar:WCHAR; // A0
          UAttributes:Word); // B0
       1:(AsciiChar:CHAR;    // A1
          AAttributes:Word); // B1
     end;(A0 + B0) 整体 与 (A1 + B1)共同存在与一个大的空间
    而非 A0与B0..A1与B1
      

  5.   

    TConsoleScreen LOldScreen;
    TConsoleScreen LNewScreen;
    TConsoleScreen LTmpScreen; LOldScreen = LNewScreen;最后的这句赋值语句报错,
    error C2106: '=' : left operand must be l-value;------------------TConsoleScreen  shi 什么类型 如果是数组 不能直接赋值的...
      

  6.   

    to  beyondtkl(大龙驹<*学习祖国医学中,国粹,路在何方?*>) (
    看好 WCHAR CHAR AsciiChar;这两个的差别像楼主这么定义,如果真碰见两个结构体赋值的时候会出现什么后果?
    没错,是共享一个大的内存空间,可是按照楼主的意思,结果会怎样?-     WCHAR                    -           WORD               -
    1 2 3 4 5 6 7 8-1 2 3 4 5 6 7 8-1 2 3 4 5 6 7 8-1 2 3 4 5 6 7 8
    -     CHAR     -     WORD                      -
    1 2 3 4 5 6 7 8-1 2 3 4 5 6 7 8-1 2 3 4 5 6 7 8-1 2 3 4 5 6 7 8结果如何?你最清楚了。
      

  7.   

    回复人: cnmaxu(Max) ( ) 信誉:100  2005-06-30 17:52:00  得分: 0  
     
     
       所以我才建议楼主在第二个结构体里面加一个成员,我可没有说联合的结构体必须得相等啊。
      
    ------------------------呵呵 wsm要加呢 不加当然可以了 没事的 编译器会正确赋值的.. 可以去看看C语言的描述. 像楼主这么定义,如果真碰见两个结构体赋值的时候会出现什么后果?
    -------------
    要知道 你自己没意义的赋值当然可能导致无意义的后果了.
      

  8.   

    to  beyondtkl(大龙驹<*学习祖国医学中,国粹,路在何方?*>) 
    知道就好。
      

  9.   

    HOHO 这点 区区还是知道滴.
      

  10.   

    typedef struct _CHAR_INFO { 
      union {
          WCHAR UnicodeChar; 
          CHAR AsciiChar; 
      } Char; 
      WORD Attributes; 
    } CHAR_INFO, *PCHAR_INFO; typedef _CHAR_INFO TCharInfoExT[25][80];
      

  11.   

    楼主的例子很巧合的正好是UnicodeChar对应AsciiChar, UAttributes对应AAttributes, 它们在地址上是一致的, 如果定义成packed record的话才会出现cnmaxu(Max)说的情况