• Delphi 记录Record和字符串String相互赋值


    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      PUpData = ^TUpData;
    
      TUpData = record
        a1: array[1..2] of char;
        a2: array[1..3] of char;
        a3: array[1..4] of char;
      end;
    
    type
      TForm1 = class(TForm)
        btn1: TButton;
        btn2: TButton;
        procedure btn1Click(Sender: TObject);
        procedure btn2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    //记录内容赋值给字符串
    procedure TForm1.btn1Click(Sender: TObject);
    var
      s: string;
      p: pchar;
      UpData: TUpData;
    begin
      //初始化记录
      ZeroMemory(@UpData, SizeOf(UpData));
    
      //赋值
      UpData.a1 := 'a1';
      UpData.a2 := 'a22';
      UpData.a3 := 'a333';
    
      //分配字符串长度
      setlength(s, SizeOf(UpData));
    
      //记录内容赋值给字符串
      p := StrLCopy(pchar(s), @UpData, SizeOf(UpData));
    
      //显示结果  a1a22a333|9|9
      showmessage(s + '|' + InttoStr(length(s)) + '|' + InttoStr(SizeOf(UpData)));
    end;
    
    //字符串内容赋值给记录
    procedure TForm1.btn2Click(Sender: TObject);
    var
      s: string;
      p: pchar;
      UpData: TUpData;
    begin
      //初始化记录
      ZeroMemory(@UpData, SizeOf(UpData));
    
      //赋值
      s := 'sendinfo.suv';
    
      //字符串内容赋值给记录
      p := StrLCopy(@UpData, pchar(s), sizeof(TUpData));
    
      //显示结果 sendinfo.
      showmessage(UpData.a1 + UpData.a2 + UpData.a3);
    end;
    
    end.
    

      

  • 相关阅读:
    SQL实战(四)
    SQL实战(三)
    SQL实战(二)
    数据库SQL实战(一)
    算法(二)——背包问题
    华为机试(五)
    算法(一)
    华为机试练习(四)
    华为往年机试题目(三)
    T分布(T-Distribution)
  • 原文地址:https://www.cnblogs.com/studycode/p/11301558.html
Copyright © 2020-2023  润新知