Hi,
You can not do what you are writing:
Program ArrayPointer;
Type Strname = string[100];
Var Pstr : ^Strname;
Begin
New (Pstr);
Pstr[0]:='ABC';
Pstr[1]:='BCD';
Pstr[2]:='CDE';
Dispose (Pstr);
Writeln ('0=',Pstr[0], '; 1=',Pstr[1], '; 2=',Pstr[2]);
Readln;
End.
Well, I am not 100% sure about fpc compiler behavior, but if you compile your code with TP7 or Delphi, you will obtain:
- Compilation error (type mismatch char / string)
- Run time error
I'll explain you a little bit why:
Var Pstr : ^Strname; Here you declare a variable of type pointer. So the content of this variable is nothing more than a memory address.
Pstr[0]:= 'ABC'; Here, delphi compiler will give you an error. First of all, you try to assign a string to the variable Pstr, Pstr is a pointer => type mismatch.
The good codding for this should be:
Pstr^[0]:= 'ABC'; But here also, delphi compiler will gives you an error. Pstr^ is a string of 100 Chars., so Pstr^[0] is one char => you try to assign a string to a Char => type mismatch.
What will work is:
Pstr^:='ABC'; In this case, Pstr^[0] will be equal to 'A' ... Pstr^[2]='C';
I hope you understand what I wanted to explain you, again, I am not 100% sure about the syntax fpc allows (maybe when you do Pstr[0] you really assign Pstr^[0], but it is not the "pascal standard" and if you try your codding on another compiler, this time I am 100% sure it will not compile).
Cheers.
And once again, to do what you want to do: having a list of string which can increase / decrease without knowing it maximum / minimum size, you should use the Stringlist class, everything is already codded, you will not have to manage the memory.