How to change (access to an element) an array element through a pointer to it?

var q: array of integer; Pq: pointer; begin pq:=addr(q); end; 

    1 answer 1

    Well, probably so. Maybe there is a better way, but I had to do this a couple of times. In C and C ++ with pointers to arrays (and to elements) everything is much simpler and more interesting. In my opinion, it is better not to use pointers if you can do without them.

     program Project1; {$APPTYPE CONSOLE} uses SysUtils; Var Mas: Array[0..99] Of Integer; PMas: ^Integer; i, n: Byte; begin Write(' n = '); ReadLn(n); Randomize; For i:=0 To N-1 Do Begin // PMas:=Addr(Mas[i]); можно написать так PMas:= @Mas [i]; PMas^:=Random(100)-Random(100); WriteLn(' ', PMas^); End; ReadLn; end. 
    • Random (100) was enough for the demonstration. If you needed numbers from -100 to 100, you could make Random (200) -100, and Random (100) -Random (100) gives an uneven distribution. - insolor
    • But what difference does the main example show. I just got used to writing, but I did not think about this mathematics. - DelphiM0ZG
    • For a demonstration, you could, for example, write some specific number at PMas address, and then show that it is exactly in Mas [i]. But yes, it's all the little things PS In any case, it was interesting to learn how it is done in Delphi - insolor