delphi二维数组初始化并输出的问题
代码如下,本文delphi自己没事学的。指针刚开始看。unitu_Main;interfaceusesWindows,Messages,SysUtils,Variants...
代码如下,本文delphi自己没事学的。指针刚开始看。
unit u_Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
ArrayType = array[0..3] of Integer;
pArray = ^ArrayType;
TForm1 = class(TForm)
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure fun(Test: array of ArrayType);
var
I, J: Integer;
begin
for I := 0 to High(Test) do
for J := 0 to High(ArrayType) do
ShowMessage(IntToStr(Test[I, J]));
end;
procedure TForm1.btn1Click(Sender: TObject);
var
ArrayType1: array [0..2] of ArrayType;
Array_Pointer: pArray;
J: Integer;
begin
for I := 0 to High(ArrayType1) do
for J := 0 to High(ArrayType) do
ArrayType1[I, J] := I * J;
Array_Pointer := @ArrayType1;
fun(Array_Pointer^);
end;
end.
麻烦大神帮我改一下btn1Click中的代码,能让二维数组输出全部元素。比如二维是Test[2][3]. 展开
unit u_Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
ArrayType = array[0..3] of Integer;
pArray = ^ArrayType;
TForm1 = class(TForm)
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure fun(Test: array of ArrayType);
var
I, J: Integer;
begin
for I := 0 to High(Test) do
for J := 0 to High(ArrayType) do
ShowMessage(IntToStr(Test[I, J]));
end;
procedure TForm1.btn1Click(Sender: TObject);
var
ArrayType1: array [0..2] of ArrayType;
Array_Pointer: pArray;
J: Integer;
begin
for I := 0 to High(ArrayType1) do
for J := 0 to High(ArrayType) do
ArrayType1[I, J] := I * J;
Array_Pointer := @ArrayType1;
fun(Array_Pointer^);
end;
end.
麻烦大神帮我改一下btn1Click中的代码,能让二维数组输出全部元素。比如二维是Test[2][3]. 展开
若以下回答无法解决问题,邀请你更新回答
展开全部
指针的类型与数组的类型不一致,数组是array [0..2] of ArrayType类型,而指针是指向ArrayType的,也就是指针只是指向数组的元素的,而不是指向数组,导致函数调用传递参数不正确。既然函数FUN是用传值方式,就没有必要转换成指针,再由指针又转换成变量(数组),这多余了。
procedure TForm1.btn1Click(Sender: TObject);
var
ArrayType1: array [0..2] of ArrayType;
//Array_Pointer: pArray;
J: Integer;
begin
for I := 0 to High(ArrayType1) do
for J := 0 to High(ArrayType) do
ArrayType1[I, J] := I * J;
//Array_Pointer := @ArrayType1;
fun(ArrayType1);
end;
如果要用传址方式,array [0..2] of ArrayType必须在函数FUN之前定义成类型,再改形参和实参。
procedure TForm1.btn1Click(Sender: TObject);
var
ArrayType1: array [0..2] of ArrayType;
//Array_Pointer: pArray;
J: Integer;
begin
for I := 0 to High(ArrayType1) do
for J := 0 to High(ArrayType) do
ArrayType1[I, J] := I * J;
//Array_Pointer := @ArrayType1;
fun(ArrayType1);
end;
如果要用传址方式,array [0..2] of ArrayType必须在函数FUN之前定义成类型,再改形参和实参。
追问
亲,传址方式要怎么改呢。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询