zoukankan      html  css  js  c++  java
  • 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.
    

      

  • 相关阅读:
    30-语言入门-30-分数加减法
    29-语言入门-29-两点距离
    bootstrapcss3触屏滑块轮播图
    input输入样式,动画
    HTML5夜空烟花绽放动画效果
    精美留言、评论框,带微博表情
    Sublime Text 3汉化中文版
    直播英国脱欧各国反应?谁将是最大赢家?
    品牌关键字的重要性?是什么呢
    网站收录之网络推广
  • 原文地址:https://www.cnblogs.com/studycode/p/11301558.html
Copyright © 2011-2022 走看看