type
  TDataUnit = record
    Len: Byte;                       //参数长度
    Obj_ID: Word;                    //参数ID
    Content: Array of Byte;          //参数内容
    DataType: String;                //数据类型
  end;
我定义了这样一个结构体,想用copymemory进行简洁的赋值,可以吗?请给出具体的说明一下。这个id注册了很多年,以前的csdn是以天计算可用分的,不登录也行,导致我分太多了,发点出来,大家拿

解决方案 »

  1.   

     我觉得不行,应定义成这样
     type 
      TDataUnit = record 
        Len: Byte;                      //参数长度 
        Obj_ID: Word;                    //参数ID 
        Content: Array of Byte;          //参数内容 
        DataType: Array of Char;         //数据类型 
      end; 然后再copyMemoryprocedure TForm1.Button1Click(Sender: TObject);
    var
      DataA, DataB: TDataUnit;
    begin
      copyMemory(@DataA, @DataB, sizeof(TDataUnit));
    end;
      

  2.   

    如果需要结构体的序列化和拷贝,不能带有不可预知大小的成员。Content: Array of Byte;  DataType: Array of Char; 
      

  3.   

        Content: Array of Byte;          //参数内容
        DataType: String;                //数据类型 这两个不定长的,处理起来可能会出错,建议改成定长的再用
    copyMemory(@DataA, @DataB, sizeof(TDataUnit)); 
    例子
      PSMSinfo = ^TSMSInfo;
      TSMSInfo = packed record
        sComPort: string[5]; //数据库地址
        BaudRate: string[4]; //数据库名称
        Center:   string[20];//数据库密码
      end;
      

  4.   

    就是,应该定长,我前面写错了!汗我觉得不行,应定义成这样 
    type 
      TDataUnit = record 
        Len: Byte;                      //参数长度 
        Obj_ID: Word;                    //参数ID 
        Content: Array[0..255] of Byte;          //参数内容 
        DataType: Array [0..255]of Char;        //数据类型 
      end; 
      

  5.   


    type 
      TDataUnit = record 
        Len: Byte;                      //参数长度 
        Obj_ID: Word;                    //参数ID 
        Content: Array[0..請指定長度] of Byte;          //参数内容 
        DataType: array[0..請指定長度] of char;                //数据类型 
      end;