Forum > General
pointer to pointer
(1/1)
airpas:
hi
look at this example , the procedure changestr works only if the argument is ppchar , and not for pchar
--- Code: ---
uses strings;
type
ppchar = ^pchar;
procedure changestr(s : ppchar);
begin
getMem(s^,16);
strcopy(s^,'Hello World');
end;
var
p : pchar;
begin
changestr(@p);
writeln(p);
readln;
end.
--- End code ---
why it doesn't work when using only one pointer to char e.g
--- Code: ---
uses strings;
type
ppchar = ^pchar;
procedure changestr(s : pchar);
begin
getMem(s,16);
strcopy(s,'Hello World');
end;
var
p : pchar;
begin
changestr(p);
writeln(p); // print nothing !!
readln;
end.
--- End code ---
eny:
In the first example you 'force' a parameter by reference call.
In the second a parameter by value hence there is no returning value stored in p.
You probably create a memory leak though.
howardpc:
This returns a value and avoids the memory leak
--- Code: ---program project1;
uses strings;
procedure changestr(var s : pchar);
begin
GetMem(s,16);
strcopy(s,'Hello World');
end;
var
p : PChar;
begin
changestr(p);
WriteLn('"',p,'"');
Freemem(p, 16);
ReadLn;
end.
--- End code ---
Navigation
[0] Message Index