【问题标题】:What does "type PDW = ^dword;" mean in delphi?"type PDW = ^dword;" 是什么意思?在德尔福是什么意思?
【发布时间】:2018-06-29 05:36:02
【问题描述】:

在将 delphi 代码转换为 C 时,我发现了一些复杂点。

下面是delphi的完整代码

unit resizeunit;

{ resize bm1 to fit in bm2
  bm1,bm2 bitmaps must be created and width,height set }

interface

uses windows,graphics;

procedure BMresize;

var bm1,bm2 : Tbitmap;

implementation

type PDW = ^dword;

procedure BMresize;
//copy bm1 to bm2
var ps0,pd0,psStep,pdStep : dword;       //scanline[0], row steps
    sx1,sy1,sx2,sy2 : single;             //source field positions
    x,y,i,j,destwidth,destheight : word;  //source,dest field pixels
    destR,destG,destB : single;           //destination colors
    sR,sG,sB : byte;                      //source colors
    fx,fy,fix,fiy,dyf : single;           //factors
    fxstep,fystep, dx,dy : single;
    color : dword;
    pdy,pdx,psi,psj : dword;
    AP : single;
    istart,iend,jstart,jend : word;
    devX1,devX2,devY1,devY2 : single;
begin
 ps0 := DWORD(bm1.scanline[0]);
 psstep := ps0 - DWORD(bm1.scanline[1]);
 pd0 := DWORD(bm2.scanline[0]);
 pdstep := pd0 - DWORD(bm2.scanline[1]);
 destwidth := bm2.Width-1;
 destheight := bm2.Height-1;
 fx := bm1.width/ bm2.width;
 fy := bm1.height/bm2.height;
 fix := 1/fx;
 fiy := 1/fy;
 fxstep := 0.9999 * fx;
 fystep := 0.9999 * fy;
 pdy := pd0;
 for y := 0 to destheight do         //vertical destination pixels
  begin
   sy1 := fy * y;
   sy2 := sy1 + fystep;
   jstart := trunc(sy1);
   jend := trunc(sy2);
   devY1 := 1-sy1+jstart;
   devY2 := jend+1-sy2;
   pdx := pdy;
   for x := 0 to destwidth do        //horizontal destination pixels
    begin
     sx1 := fx * x;                        //x related values are repeated
     sx2 := sx1 + fxstep;                  //for each y and may be placed in
     istart := trunc(sx1);                 //lookup table
     iend := trunc(sx2);                   //...
     devX1 := 1-sx1+istart;                  //...
     devX2 := iend+1-sx2;                  //...
     destR := 0; destG := 0; destB := 0;   //clear destination colors
     psj := ps0-jstart*psStep;
     dy := devY1;
     for j := jstart to jend do  //vertical source pixels
      begin
       if j = jend then dy := dy - devY2;
       dyf := dy*fiy;
       psi := psj + (istart shl 2);
       dx := devX1;
       for i := istart to iend do //horizontal source pixels
        begin
         if i = iend then dx := dx - devX2;
         AP := dx*dyf*fix;
         color := PDW(psi)^;
         sB := color;
         destB := destB + sB*AP;
         sG := color shr 8;
         destG := destG + sG*AP;
         sR := color shr 16;
         destR := destR + sR*AP;
         inc(psi,4);
         dx := 1;
        end;//for i
       dec(psj,psStep);
       dy := 1;
      end;//for j
      sB := round(destB);
      color := sB or (sG shl 8) or (sR shl 16);
     PDW(pdx)^ := color;
     inc(pdx,4);
    end;//for x
   dec(pdy,pdstep);
  end;//for y
end;

end.

我正在努力理解

type PDW = ^dword; ,inc(psi,4); and dec(psj,psStep); 

意思。

添加

我想知道 color = PDW(psi) ^ 的含义; 如果我想应用一些数组变量,那我应该怎么做?

这是来自德尔福代码。我正在尝试在 C 中进行转换。 我以前从未接触过delphi代码。

您能帮我理解它们的含义以及在 C 中的转换吗?

【问题讨论】:

  • PDW = ^dword;PDW 类型的声明,作为指向DWORD 的指针。 inc(psi,4); 是将psi 变量的值增加4。dec(psj,psStep); 是将psj 变量的值减少psStep 变量的值。
  • @Victoria 我还是不明白。你能举例让我知道你的评论吗?
  • 为什么不呢,比如typedef DWORD *PDW;

标签: delphi


【解决方案1】:

就像维多利亚说的

type PDW = ^DWORD;

等价于

typedef DWORD* PDW;

还有

inc(psi,4);

等价于

psi+=4;

同样

dec(psj,psStep);

等价于

psj-=psStep;

终于

color = PDW(psi)^

等价于

color = *((PDW)psi); // I think. It reads the DWORD value at address psi.

(以上所有仅对代码中显示的变量定义有效)

【讨论】:

  • PDW 是 C 中的某种数组,例如 'unsigned char tmp[]' 吗?
  • 指向数据的指针可以被视为指向数组的数组。我建议你了解指针算法及其工作原理。
  • @RemyLebeau:不完全是。 Variables 属于 type “指向数据的指针”可以用作数组。不是类型定义本身。 PDW 不是变量 - 它是一种类型,因此不是数组。但是该类型的variables 可以用作基本类型的一维数组(在这种情况下为 DWORD)。
  • @HeartWare:雷米是正确的。指向数据的指针可以被视为指向数组的指针。指针是变量,即类型ˋPointerˋ或类型化指针类型的实例。
  • @RudyVelthuis:是的,但是PDW 不是指向数据的指针——它是一个类型定义。因此,PDW 不是“C 中的某些类型(原文如此)的数组,例如......”。 psi(它是 type PDW),但是,“指向数据的指针”,可以用作 DWORD 的从零开始的数组。