Forum > General
double pointers
fcu:
Hi
working with double pointers in pascal is something that i have never tried before
i tried converting this simple code to pascal but it seems difficult some how
could some one please show as how this code look like in pascal ?
--- Code: C [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---#include <stdio.h> int ** ptr; int main(){// allocate ptr = new int*[8]; for (int i=0; i<8; i++) ptr[i] = new int[8]; for (int i=0; i<8; i++) for (int j=0; j<8; j++) ptr[j][i] = j; // print for (int i=0; i<8; i++){ for (int j=0; j<8; j++){ printf("%d", ptr[j][i]); } printf("\n"); } // free memory for (int i=0; i<8; i++) delete ptr[i]; delete [] ptr; return 0;}
d2010:
--- Quote from: fcu on January 31, 2025, 02:52:27 pm ---Hi
working with double pointers in pascal is something that i have never tried before
i tried converting this simple code to pascal but it seems difficult some how
could some one please show as how this code look like in pascal ?
--- Code: C [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---#include <stdio.h> int ** ptr; int main(){// allocate ptr = new int*[8]; for (int i=0; i<8; i++) ptr[i] = new int[8]; for (int i=0; i<8; i++) for (int j=0; j<8; j++) ptr[j][i] = j; } // free memory for (int i=0; i<8; i++) delete ptr[i]; delete [] ptr; return 0;}
--- End quote ---
Your source inside C++ is worst, even with VC++ , but ?? in Delphi/Lazarus.
You do not make 100* getmem or 100* new(allocate-memory).
You make one, but only once getmem and only once freemem.
--- Code: ---Program ArsenieBoca_Multumesc;
{$apptype consoleapp}
Const maxC=255;
maxL=255;
Type MatrixPtr=array[0..maxL,0..maxC] of integer;
tap_Matric=^ MatrixPtr;
Var ptr0:tap_Matric
i,j:integer;
Begin
ptr0:=nil;
Randomize;
i:=00;
j:=00;
Getmem(ptr0,sizeof(MatrixPtr));
for i:=0 to 100 do
for j:=0 to 100 do
ptr0^[i,j]:=random(1203);
sum:=00;
for i:=0 to 100 do for j:=0 to 100 do sum:=sum+ptr^[i,j];
writeln('Al-sum=',sum );
readln;
freemem(ptr0);
ptr:=nil;
End;
--- End code ---
Always, you reduce numbers-of-call("freemem") and
you reduce numbers-of-call("GetMem") to minimum as posibile.
You can user dynamic of lines or columns, but count-of only < 255.
440bx:
--- Quote from: fcu on January 31, 2025, 02:52:27 pm ---Hi
working with double pointers in pascal is something that i have never tried before
i tried converting this simple code to pascal but it seems difficult some how
could some one please show as how this code look like in pascal ?
--- Code: C [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---#include <stdio.h> int ** ptr; pascal: type Ptr2Integer = ^integer; Ptr2Ptr2Integer = ^Ptr2Integer; var ptr : Ptr2Ptr2Integer; i, j : integer; int main(){ // pascal: just put the statements between the "begin" and "end."// allocate ptr = new int*[8]; // pascal: Getmem(ptr, 8 * sizeof(ptr^)); for (int i=0; i<8; i++) // pascal: for i := 0 to 7 do ptr[i] = new int[8]; // pascal: Getmem(ptr[i], 8 * sizeof(ptr[i]^)); for (int i=0; i<8; i++) // pascal: for i := 0 to 7 do for (int j=0; j<8; j++) // pascal: for j := 0 to 7 do ptr[j][i] = j; // pascal: ptr[i][j] := j; // note: the indexes in the C code are reversed, that's conceptually incorrect. // print for (int i=0; i<8; i++){ // pascal: for i := 0 to 7 do begin for (int j=0; j<8; j++){ // pascal: for j := 0 to 7 do begin printf("%d", ptr[j][i]); // pascal: write(ptr[i][j]; } // pascal: end; printf("\n"); // pascal: writeln; } // pascal: end; // free memory for (int i=0; i<8; i++) // pascal: for i := 0 to 7 do delete ptr[i]; // pascal: freemem(ptr[i], 8 * sizeof(ptr[i]^)); delete [] ptr; // pascal: freemem(ptr, 8 * sizeof(ptr^)); return 0; // pascal: nothing}
--- End quote ---
Untested but, it should be pretty close.
Note that the array indexes in the C code are reversed and the only reason the code will work is because the arrays are the same size, if they weren't that code would make a mess. The Pascal code has the indexes as they should be, therefore the Pascal code is not fully equivalent to the C code.
fcu:
aha , my fault i thought getmem works just like NEW in c++(you don't have to specify the size of the type)
now it works
thank you
440bx:
--- Quote from: fcu on January 31, 2025, 08:12:32 pm ---thank you
--- End quote ---
You're welcome.
Navigation
[0] Message Index
[#] Next page