数组类型转换(50分)

  • 主题发起人 主题发起人 wei2003
  • 开始时间 开始时间
W

wei2003

Unregistered / Unconfirmed
GUEST, unregistred user!
请教一个问题,我有以下代码有点问题。本来我要返回一个整型值,但总报这个错,[Error] myfunction.pas(1806): Incompatible types: 'Integer'end 'TPaperNo'result:=pPapernos^[numPaperformats];
就是这句有问题,我定义pPapernos是个整型的,不太明白请赐教;


function GetPaperno:integer;
type
TPaperNo = array[0..63] of integer;
TPapernoArray = array[1..256] of TPaperNo;
FPapernoArray = ^TPapernoArray;
var
i, numPaperformats: Integer;
printername:string;
pPapernos: FPaperNoArray;
PrinterHandle: THandle;
// p_names: array[0..255] of char;
Device: array[0..255] of char;
Driver: array[0..255] of char;
Port: array[0..255] of char;
hDMode: THandle;
PDMode: PDEVMODE;
asize:integer;
begin
printername:=printer.Printers[printer.PrinterIndex];
OpenPrinter(PChar(PrinterName), PrinterHandle, nil);
Printer.GetPrinter(Device, Driver, Port, hDMode);
pDMode := GlobalLock(hDMode);
numPaperformats :=WinSpool.DeviceCapabilities(PChar(PrinterName), 'LPT1', DC_PAPERS, nil, nil);
if numPaperformats > 0 then
begin
GetMem(pPapernos,numPaperformats * Sizeof(pPapernos));
// new(pPapernos);
try
WinSpool.DeviceCapabilities(PChar(PrinterName), 'LPT1', DC_PAPERS,pchar(pPapernos), nil);
result:=pPapernos^[numPaperformats];
finally
FreeMem(pPapernos);
end;
end;
end;
 
WinSpool.DeviceCapabilities(PChar(PrinterName), 'LPT1', DC_PAPERS,pchar(pPapernos), nil);
你本來是integer,又如何可以用pchar(pPapernos)???
你改TPaperNo = array[0..63] of integer;為TPaperNo = array[0..63] of char;試一下
 
但我要的就是整型的呀,那样一改就达不到那种作用
 
TPaperNo = array[0..63] of integer;
TPapernoArray = array[1..256] of TPaperNo

你的前两句就有问题吧,TPaperNo定义为[0..63],TPapernoArray定义为[1..256],是不是超限了。
 
pPapernos^[numPaperformats];
得到的就是TPaperNo,你改为
result:=pPapernos^[numPaperformats][1];
还差不多。
 
type
TPaperNo = array[0..63] of integer;
TPapernoArray = array[1..256] of TPaperNo;
FPapernoArray = ^TPapernoArray;

function GetPaperno:FPapernoArray;
var
i, numPaperformats: Integer;
printername:string;
pPapernos: FPaperNoArray;
PrinterHandle: THandle;
// p_names: array[0..255] of char;
Device: array[0..255] of char;
Driver: array[0..255] of char;
Port: array[0..255] of char;
hDMode: THandle;
PDMode: PDEVMODE;
asize:integer;
begin
printername:=printer.Printers[printer.PrinterIndex];
OpenPrinter(PChar(PrinterName), PrinterHandle, nil);
Printer.GetPrinter(Device, Driver, Port, hDMode);
pDMode := GlobalLock(hDMode);
numPaperformats :=WinSpool.DeviceCapabilities(PChar(PrinterName), 'LPT1', DC_PAPERS, nil, nil);
if numPaperformats > 0 then
begin
GetMem(pPapernos,numPaperformats * Sizeof(pPapernos));
// new(pPapernos);
try
WinSpool.DeviceCapabilities(PChar(PrinterName), 'LPT1', DC_PAPERS,pchar(pPapernos), nil);
result:=pPapernos^[numPaperformats];
finally
FreeMem(pPapernos);
end;
end;
end;
 
可我要的是整型
 
to 远帆
但我TPaperNo = array[0..63] of char;
TPapernoArray = array[1..256] of TPaperNo;
FPapernoArray = ^TPapernoArray;
这样我可以pPapernos^[numPaperformats]它会返回一个字符串呢?
而整型不可以这样呢?
 
你定义为array[0..63] of char实际上就是一个字符串。一个整型数据串(指针)那是一个什么东西?当然不能与整型等同了。
 
pPapernos^[numPaperformats] 类型为TPaperNo = array[0..63] of integer
这本来就不是Integer,也不兼容
 
定義 pPapernos: DWord;
WinSpool.DeviceCapabilities(PChar(PrinterName), 'LPT1', DC_PAPERS,@pPapernos, nil);
就好

 
接受答案了.
 
后退
顶部