数组及指针问题(100分)

  • 主题发起人 主题发起人 tjchywxc
  • 开始时间 开始时间
T

tjchywxc

Unregistered / Unconfirmed
GUEST, unregistred user!
//定义全局变量数组 writebuf
var
writebuf: array[0..127] of char;

//函数decode将参数数组全部设为a
function decode(data:array[0..127] of char)
begin
for i:=0 to 127 do data:='a';
end;

//调用函数
decode(writebuf)
问题在于 调用此函数后,writebuf数组的值没有改变,
请问如何传递一个数组到一个函数,并且在此函数中改变传递过来数组的值,好象可以用指针,具体怎么来实现,麻烦各位大侠给改一下。
 
传递数组的地址啊,使用指针来控制下移,你这样写函数肯定不对撒!
 
能给写一下代码吗,实在是头晕啊,感谢!
 
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
writebuf: array[0..10] of char;

implementation

{$R *.dfm}
function decode(data:Pchar):Pchar;
var
i:integer;
begin
try
for i:=0 to 9 do
begin
data := 'a';
end;
result := data;
except
result := nil;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
writebuf := '0123456789';
for i:=0 to length(decode(writebuf))-1 do
begin
memo1.Lines.Add(writebuf);
end;

end;

end.
 
这样修改一下更好:
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
writebuf: array[0..10] of char;

implementation

{$R *.dfm}
function decode(data:Pchar):Pchar;
var
i:integer;
begin
try
for i:=0 to length(data)-1 do
begin
data := 'a';
end;
result := data;
except
result := nil;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
writebuf := '0123456789';
memo1.Lines.Clear;
memo1.Lines.Add(writebuf);
for i:=0 to length(decode(writebuf))-1 do
begin
memo1.Lines.Add(writebuf);
end;

end;

end.
 
请问 cqwty 我这样做怎么报以下错误
[Error] Unit1.pas(297): Incompatible types: 'Byte' and 'PAnsiChar'
 
ok了,谢谢
 
后退
顶部