Author Topic: [SOLVED]Dynamic array as pointer  (Read 834 times)

lazarusprogrammer

  • New Member
  • *
  • Posts: 35
[SOLVED]Dynamic array as pointer
« on: September 22, 2026, 12:35:01 am »
Hi, could you guys help please;

I have few two dim arrays with different sizing and i have procedure that is gonna do stuff with thoes arrays and the problem is in passing this different array to a procedure.

Seems like a decent way to do that is to use pointer. But as per usual i have no idea what i am doing. So i have this code that is almost working but now quite yet.

Arrays contains colors and seems like passing it thru a pointer is working since i am getting first color of array. But it seems like a size of array is 1x1 and not the full size of it. So the question is how to make it works.

Here is the code

Code: Pascal  [Select][+][-]
  1. procedure DrawPicture(x, y : Integer; img : Pointer);
  2. var x1, y1 : Integer;
  3.     temp   : array of array of Integer;
  4. begin
  5. temp := @img;
  6.  
  7. for x1:=Low(temp) to High(temp) do
  8.     begin
  9.     for y1:=Low(temp) to High(temp[0]) do
  10.         begin
  11.         ScaledPixel(x1 * SCALE + x, y1 * SCALE + y, temp[x1, y1], SCALE);
  12.         end;
  13.     end;
  14. Pointer(temp) := Nil;
  15. end;
  16.  
« Last Edit: September 24, 2026, 10:56:26 pm by lazarusprogrammer »

jamie

  • Hero Member
  • *****
  • Posts: 8071
Re: Dynamic array as pointer
« Reply #1 on: September 22, 2026, 01:10:21 am »
Low(Tmp [ x1 ]) to High([x1];

Your array is an array with another Array attached to each one of the first array so you must index the sub array via the index of the parent array.


 But if you want to pass that to a function you can make a type for that array..


 TMyBranchArray = Array of array of Integer;


 var
  AArray :TMyBranchArray;

that will pass to a function.

  Low(AARRAY[ 0 ]) to High(ARRAY[ 0 ]) do....

  The 0 indicates the main brance to look at the sub-branch of index 0

Hope you understand that.

Jamie
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 8071
Re: Dynamic array as pointer
« Reply #2 on: September 22, 2026, 01:19:32 am »
after looking a bit more, it seems you are trying to draw or scale a raster image ?

Jamie
The only true wisdom is knowing you know nothing

lazarusprogrammer

  • New Member
  • *
  • Posts: 35
Re: Dynamic array as pointer
« Reply #3 on: September 22, 2026, 01:27:21 am »
after looking a bit more, it seems you are trying to draw or scale a raster image ?

Jamie
i am storing image as constant array and trying to draw it using ptcgraph.
image array is looking something like this
Quote
const
Small_img : array[0..1, 0..1] of Integer =
( (16711718), (16711718),
  (16711718), (16711718));
Attached project if you wish to look at it. You can change temp to small_img or laz_img to see how it should look like.
« Last Edit: September 22, 2026, 01:43:57 am by lazarusprogrammer »

jamie

  • Hero Member
  • *****
  • Posts: 8071
Re: Dynamic array as pointer
« Reply #4 on: September 22, 2026, 02:12:48 am »
The problem you are seeing is most likely the local array does not know what the external array size is and therefor is initialized at zero basically.

if the img source is a 2 dim array then pass as such, this way you can get that info.

 otherwise, you need to pass along the width and height of the image or pass a record via reference with that information in it.

 If the Image has special header info included then that can also be used to determine the X,Y plane sizes, otherwise you won't get to far this way.

Code: Pascal  [Select][+][-]
  1. Type
  2.  TMyImageArray = Array of array of integer;
  3. procedure DrawPicture(x, y : Integer; Var img :TMyImageArray);
  4. ......
  5.  
Jamie
 
« Last Edit: September 22, 2026, 02:15:26 am by jamie »
The only true wisdom is knowing you know nothing

lazarusprogrammer

  • New Member
  • *
  • Posts: 35
Re: Dynamic array as pointer
« Reply #5 on: September 22, 2026, 03:18:17 am »
The problem you are seeing is most likely the local array does not know what the external array size is and therefor is initialized at zero basically.
 Jamie
Well i though that dimensions of array would be passed with a pointer. I mean just logically if a pointer accepting an array it is definitely should know dimensions of it. Or pointer storing 2 dim array as 1 dim array?

so i've tried doing this for y1:=Low(temp) to High(temp[0][0]) do and as result i got more colors form the picture but they got printed vertically and i got got access violation error.

So this tells me that the pointer knows all the colors that array have. So apparently it's possible to make it work. I could pass array dimensions to procedure but just for test i set length of this temp array and it didn't change anything.

And about passing array as a type yes gonna work but it's not suitable for me for my needs.
« Last Edit: September 22, 2026, 03:36:36 am by lazarusprogrammer »

jamie

  • Hero Member
  • *****
  • Posts: 8071
Re: Dynamic array as pointer
« Reply #6 on: September 22, 2026, 04:45:44 am »
Not knowing the type of array that is being offered up via the img pointer makes it hard to make a determination.

The array you are attempting to use is a managed array, which means there is some overhead and if you simply mask an outside pointer to it, it will most like do all sorts of strange things.

 If the img pointer is basically just that, a pointer to a blob of memory, then you need to supply a X,Y size.

 You can create arrays to with unmanageable pointers via GetMem for example, but you need to calculate the required memory by doing the X*Y*SizeOfPixelData.
 
 Then you can pick one of the various pointers already defined in the system, for example PInteger;

Var
 P :PInteger;
BEgin
  P := img;
  P[ X + (Y*NUMBER_OF_PIXS_PERLINE) ]:=ColorValue;

Color value being an integer for example.

Jamie


 
The only true wisdom is knowing you know nothing

Zvoni

  • Hero Member
  • *****
  • Posts: 3595
Re: Dynamic array as pointer
« Reply #7 on: September 22, 2026, 08:00:19 am »
Well i though that dimensions of array would be passed with a pointer. I mean just logically if a pointer accepting an array it is definitely should know dimensions of it. Or pointer storing 2 dim array as 1 dim array?
No. A pointer is just that: A single address in Memory.
The pointer itself doesn't know about reserved memory, dimensions and whatever else.

I'd go with Jamie's suggesttion creating a type, and using that type in the Argument-List.

If you want to go the Pointer-Route, i would use PInteger instead of Pointer as arg-type, since you can actually dereference a Pinteger, while a pointer you cannot, at least not directly.
you would pass the address of the very first array-element: @MyArray[0,0]
additionally to the pointer pass the size(s) of the array.

And do remember: a 2D-Array of Integer IS actually in continous Memory-Space, so you could even handle it as a 1D-Array, though i don't remember if it's columns first, rows second or the other way around.
Would have to look at the memory-layout

EDIT: Better said: An Array (irrespective how many dimensions) is ALWAYS a continous range in Memory, but depending on the Element-type (e.g. Integers) the values themselves are stored there, or (e.g. strings, Objects and similar) their pointers are stored there.
So theoretically, any Array with how many ever dimensions, can be accessed as a 1D-Array. You only have to take care of the Type and Memory-Layout

and FWIW:
Code: Pascal  [Select][+][-]
  1. procedure DrawPicture(x, y : Integer; img : Pointer);
  2. var x1, y1 : Integer;
  3.     temp   : array of array of Integer;
  4. begin
  5. temp := @img;
you're actually assigning a "Pointer to a Pointer"
« Last Edit: September 22, 2026, 08:29:26 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

lazarusprogrammer

  • New Member
  • *
  • Posts: 35
Re: Dynamic array as pointer
« Reply #8 on: September 22, 2026, 02:34:03 pm »
I'd go with Jamie's suggesttion creating a type, and using that type in the Argument-List.

This is like a meme from a cartoon where some jellyish thing saying maybe i have no brains but i have an idea.

So the idea is to have an image you can use in a program(game) without needs of loading it from a file and ship it with a program.

For one image it's simple i could just hard code the image name to draw procedure. But for multiple different images it needs something else.

So types is not gonna work because i can't add same type to every picture it would be duplicate and i can't access any types from picture besides standart types. Only if there is some standart types but couldn't find any that would fit.

I thought pointer would be great and easy solution. But seems like it's not.

I have another idea to store the array as array of strings where each string would look like 'x,y,color'. Maybe this would be easier to use.

Zvoni

  • Hero Member
  • *****
  • Posts: 3595
Re: Dynamic array as pointer
« Reply #9 on: September 22, 2026, 04:01:48 pm »
I'd go with Jamie's suggesttion creating a type, and using that type in the Argument-List.

This is like a meme from a cartoon where some jellyish thing saying maybe i have no brains but i have an idea.

So the idea is to have an image you can use in a program(game) without needs of loading it from a file and ship it with a program.

For one image it's simple i could just hard code the image name to draw procedure. But for multiple different images it needs something else.

So types is not gonna work because i can't add same type to every picture it would be duplicate and i can't access any types from picture besides standart types. Only if there is some standart types but couldn't find any that would fit.

I thought pointer would be great and easy solution. But seems like it's not.

I have another idea to store the array as array of strings where each string would look like 'x,y,color'. Maybe this would be easier to use.
You misunderstand

it's this
Code: Pascal  [Select][+][-]
  1. Type
  2.  TMyImageArray = Array of array of integer;  //<-- THIS ONE!!
  3. procedure DrawPicture(x, y : Integer; Var img :TMyImageArray);

This is still a dynamic 2D-Array!
you can still throw "different" images against the procedure, as long as the Array-Argument is "known" before passing it to the procedure.

For one image you pass a 20x40-Array, for the next a 78x23-Array
The Argument accepts both images, and you would even have the sizes of the array passed along (Keyword: High- resp. Length-Function)

Proof of Concept
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode ObjFPC}{$H+}
  3. USes Sysutils;
  4. Type
  5.   TMy2DIntArray=Array Of Array Of Integer;
  6.  
  7. Var
  8.   a:TMy2DIntArray;
  9.   i,j,x:Integer;
  10. procedure DrawPicture(x, y : Integer; img : TMy2DIntArray);
  11. Var x1,y1:Integer;
  12.   temp:Array Of Array Of Integer;
  13. Begin
  14.   temp:=img;
  15.   For x1:=Low(temp) To High(Temp) Do
  16.     For y1:=Low(temp[x1]) To High(temp[x1]) Do
  17.       Begin
  18.         Writeln('Element ',x1,'x',y1,' = ',temp[x1,y1]);
  19.       end;
  20. End;
  21. begin
  22.   SetLength(a,2,3);   //2x3 Array
  23.   x:=1;
  24.   for i:=Low(a) To High(a) Do
  25.     For j:=Low(a[i]) To High(a[i]) Do
  26.       Begin
  27.         a[i,j]:=x;
  28.         Inc(x);
  29.       end;
  30.   DrawPicture(0,0,a);
  31.   Writeln('------------------------------');
  32.   SetLength(a,0,0);  //Erase old Array
  33.   SetLength(a,5,8);  //5x8-Array
  34.   x:=100;
  35.   for i:=Low(a) To High(a) Do
  36.     For j:=Low(a[i]) To High(a[i]) Do
  37.       Begin
  38.         a[i,j]:=x;
  39.         Inc(x);
  40.       end;
  41.   DrawPicture(0,0,a);
  42.   ReadLn;
  43. end.
You wouldn't even need the "temp"-variable inside your Procedure
« Last Edit: September 22, 2026, 04:13:08 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Zvoni

  • Hero Member
  • *****
  • Posts: 3595
Re: Dynamic array as pointer
« Reply #10 on: September 22, 2026, 04:43:24 pm »
I looked at your Source-code.
Don't use static Arrays for your Consts.
Then it works as advertised

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode ObjFPC}{$H+}
  3. USes Sysutils;
  4. const      //CHANGE THIS!!!
  5. Small_img : array Of Array of integer =
  6. (
  7. (16711718, 16711718, 16711718, 16711718, 16711718, 16711718, 16711718, 16711718),
  8. (16711718, 6356736, 6356736, 6356736, 6356736, 6356736, 6356736, 16711718),
  9. (16711718, 6356736, 12287, 12287, 12287, 12287, 6356736, 16711718),
  10. (16711718, 6356736, 12287, 0, 0, 12287, 6356736, 16711718),
  11. (16711718, 6356736, 12287, 0, 0, 12287, 6356736, 16711718),
  12. (16711718, 6356736, 12287, 12287, 12287, 12287, 6356736, 16711718),
  13. (16711718, 6356736, 6356736, 6356736, 6356736, 6356736, 6356736, 16711718),
  14. (16711718, 16711718, 16711718, 16711718, 16711718, 16711718, 16711718, 16711718)
  15. );
  16.            //CHANGE THIS!!!
  17. laz_img : array Of Array of integer =
  18. (
  19. (16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215),
  20. (16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 2241382, 2110560, 2044766, 2176351, 2044765, 2044766, 2110560, 2829141, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215),
  21. (16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 2829141, 2175838, 2242399, 2373987, 2705015, 2903679, 3167882, 3167881, 2838144, 2639222, 2243170, 2176350, 2175838, 2829141, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215),
  22. (16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 128, 2176350, 2307423, 2968702, 5536686, 8561614, 10598617, 11255261, 11451868, 11123161, 10071764, 8890321, 6854345, 4552362, 2771579, 2242143, 2044766, 128, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215),
  23. (16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 2829141, 2110559, 2771061, 6523320, 11650273, 13293032, 13029606, 12635107, 12175074, 11780575, 11320284, 10859993, 10336475, 9747165, 9223135, 7646680, 4554163, 2573940, 2110558, 2829141, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215),
  24. (16777215, 16777215, 16777215, 16777215, 16777215, 2241374, 2176350, 3627659, 10730971, 14015982, 13884396, 13555690, 13095399, 12635108, 12175073, 11714782, 11254491, 10794714, 10336732, 9812959, 9354721, 8765411, 8241124, 6202837, 3036807, 2111070, 2241374, 16777215, 16777215, 16777215, 16777215, 16777215),
  25. (16777215, 16777215, 16777215, 16777215, 2829141, 2176351, 4549533, 12373477, 14607601, 14410223, 13949932, 13489897, 13029606, 12569572, 11648472, 5199203, 6713475, 9149626, 10271196, 9747423, 9289186, 8765668, 8307431, 7652072, 6336989, 3368601, 2110815, 2829141, 16777215, 16777215, 16777215, 16777215),
  26. (16777215, 16777215, 16777215, 0, 2242144, 3758986, 12834023, 15067892, 14804722, 14344431, 13884396, 13424105, 12963814, 12503779, 12043488, 5067617, 0, 0, 2041132, 8101563, 9158370, 8700132, 8176359, 7718122, 7128811, 5944545, 2905738, 2110303, 0, 16777215, 16777215, 16777215),
  27. (16777215, 16777215, 16777215, 2175837, 2836597, 11519712, 15396342, 15199220, 14738929, 14278638, 13818603, 13358312, 12898022, 12437987, 11977696, 5528426, 0, 0, 0, 988184, 8565717, 8634597, 8111079, 7652842, 7129068, 6604782, 5090010, 2508147, 2175837, 16777215, 16777215, 16777215),
  28. (16777215, 16777215, 2368621, 2307423, 6983611, 15593718, 15593463, 15133428, 14673137, 14213102, 13752811, 12371160, 11516366, 12306401, 11911903, 10398920, 460809, 0, 0, 0, 6523557, 8569317, 8045544, 7587306, 7063533, 6540015, 6015216, 3767988, 2242143, 2829141, 16777215, 16777215),
  29. (16777215, 16777215, 2175838, 3100543, 13425644, 15856632, 15527926, 15067635, 6514027, 3948098, 1645085, 65793, 0, 2632497, 10793164, 11386076, 6318461, 0, 0, 0, 7644097, 8503782, 7980008, 7456235, 6998253, 6474480, 5950449, 4962020, 2574972, 2045021, 16777215, 16777215),
  30. (16777215, 16777215, 2242399, 6194353, 15856888, 15856633, 15462134, 15001843, 12501710, 2303015, 0, 0, 0, 0, 4936029, 11320284, 10794456, 3357768, 131587, 4743283, 8896483, 8372710, 7914729, 7390955, 6932718, 6408944, 5950963, 5360626, 3504047, 2176607, 16777215, 16777215),
  31. (16777215, 2373467, 2439780, 10270937, 16054010, 15791096, 15330805, 14936050, 14476015, 5790562, 0, 0, 0, 0, 3751495, 11254491, 10794970, 10205403, 9483736, 9289185, 8633312, 3362654, 1187107, 4091523, 6867182, 6343665, 5885427, 5361141, 4104917, 2177636, 2571106, 16777215),
  32. (16777215, 2110813, 2770807, 13557228, 16054010, 15725304, 15265013, 14870258, 14410223, 13818346, 10133679, 3948357, 0, 0, 7371149, 11188699, 10729434, 9876438, 6520213, 4875640, 1911346, 0, 0, 0, 4094609, 6278129, 5754355, 5230325, 4504808, 2443126, 2241886, 16777215),
  33. (16777215, 2045279, 3035008, 14871540, 16054010, 15659511, 15199220, 14739185, 14344431, 13884140, 13424105, 12963814, 9805490, 8226457, 11583197, 11122906, 10598104, 2633785, 0, 0, 0, 0, 0, 0, 1915203, 6212593, 5689076, 5230582, 4507121, 2641793, 2110301, 16777215),
  34. (16777215, 2110559, 3430538, 15922682, 16054010, 15593719, 15067635, 14673393, 8685713, 3355961, 6317165, 6909562, 8028562, 11911903, 11517405, 11057370, 7833762, 0, 0, 0, 0, 0, 0, 0, 1584952, 6147314, 5623540, 5165303, 4640245, 2774410, 2110302, 16777215),
  35. (16777215, 2110559, 3430538, 15856889, 15988217, 15527926, 12567500, 3290167, 1052946, 0, 0, 0, 0, 7831699, 11451612, 10991577, 4739939, 0, 0, 0, 0, 0, 0, 0, 2576222, 6081778, 5558005, 5034231, 4640245, 2774410, 2110302, 16777215),
  36. (16777215, 2045279, 3035008, 14739955, 15856633, 15462134, 15002099, 11515070, 1250325, 0, 0, 0, 0, 6383737, 11386076, 10925785, 3160131, 0, 0, 0, 0, 0, 0, 0, 3633543, 5950706, 5492725, 4968696, 4507121, 2641536, 2110301, 16777215),
  37. (16777215, 2110813, 2770807, 13359851, 15725303, 15396342, 14936307, 14476016, 12239311, 2434858, 0, 0, 394759, 9871802, 11320283, 10860249, 4608609, 0, 0, 0, 0, 0, 0, 0, 5153730, 5885427, 5427189, 4903159, 4439272, 2443126, 2241886, 16777215),
  38. (16777215, 2373467, 2439780, 10073559, 15593719, 15330805, 14870514, 14410223, 13950188, 13160932, 7830923, 5725287, 9542576, 11714782, 11254491, 10794970, 9612749, 1975853, 0, 0, 0, 0, 0, 594197, 6343408, 5819891, 5361654, 4837367, 4104918, 2112099, 2571106, 16777215),
  39. (16777215, 16777215, 2307678, 6062768, 15330805, 15265013, 14804722, 14344687, 13884396, 13424105, 12964070, 12503779, 12109024, 9213871, 4738653, 1777445, 7242653, 9286870, 2042419, 0, 0, 0, 0, 2972010, 6278129, 5754356, 5230838, 4706038, 3373230, 2176607, 16777215, 16777215),
  40. (16777215, 16777215, 2175838, 3034493, 13096424, 15067891, 14738929, 14278894, 13818604, 13358313, 12898278, 11385039, 3422015, 0, 0, 0, 1448479, 9616608, 6193304, 0, 0, 0, 132101, 5680332, 6147057, 5689076, 5099510, 4439016, 2640764, 2045021, 16777215, 16777215),
  41. (16777215, 16777215, 2829141, 2307680, 7049660, 14673393, 14673137, 14213102, 13752811, 13292776, 8225938, 3948617, 0, 0, 0, 0, 263430, 9551072, 8763355, 1318436, 0, 66050, 3960966, 6605551, 6081778, 5623540, 5033204, 3572408, 2176607, 2829141, 16777215, 16777215),
  42. (16777215, 16777215, 16777215, 2176351, 2836341, 10665179, 14344687, 14147310, 13687019, 13226984, 10331833, 2500910, 0, 0, 0, 0, 4345952, 9485536, 8962019, 8305889, 6397371, 6730195, 6998253, 6540016, 6016242, 5491956, 4435165, 2442611, 2176351, 16777215, 16777215, 16777215),
  43. (16777215, 16777215, 16777215, 21845, 2242144, 3693453, 11913698, 13949932, 13621226, 13161191, 12700900, 12240866, 9213871, 6055030, 4738910, 6583435, 9812700, 9420257, 8896483, 8438246, 7914472, 7456491, 6932718, 6474480, 5884914, 4962534, 2906253, 2110560, 128, 16777215, 16777215, 16777215),
  44. (16777215, 16777215, 16777215, 16777215, 2241382, 2176351, 4483483, 11584479, 13358313, 13095399, 12635108, 12175073, 11714782, 11254491, 10794714, 10336732, 9812959, 9354721, 8830948, 8372966, 7849193, 7390955, 6867182, 6277615, 5355748, 3171739, 2111071, 2373467, 16777215, 16777215, 16777215, 16777215),
  45. (16777215, 16777215, 16777215, 16777215, 16777215, 2241374, 2241887, 3627657, 9744597, 12701156, 12503779, 12109281, 11648990, 11188955, 10729434, 10271196, 9747423, 9289186, 8765668, 8241895, 7783657, 7194347, 6670060, 5155290, 2905994, 2110816, 2307425, 16777215, 16777215, 16777215, 16777215, 16777215),
  46. (16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 2373467, 2110558, 2770291, 5932213, 10335960, 11780574, 11451868, 11057625, 10598106, 10205661, 9682143, 9158114, 8634595, 8110566, 7586535, 6271966, 4161718, 2442612, 2110559, 2373467, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215),
  47. (16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 0, 2110559, 2242144, 2837371, 5010858, 7641031, 9218000, 9678806, 9811674, 9353437, 8303836, 7319001, 5806541, 4093099, 2706299, 2176607, 2110557, 0, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215),
  48. (16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 2368621, 2045021, 2176351, 2308707, 2639478, 2837887, 3036809, 3036809, 2772351, 2573941, 2243171, 2241887, 2045021, 2368621, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215),
  49. (16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 2373467, 2241886, 2110301, 2110815, 2110815, 2110301, 2241886, 2571106, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215),
  50. (16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215, 16777215)
  51. );
  52.  
  53.  
  54. Type
  55.   TMy2DIntArray=Array Of Array Of Integer;
  56.  
  57. procedure DrawPicture(x, y : Integer;img : TMy2DIntArray;AName:String);
  58. Var x1,y1:Integer;
  59. Begin
  60.   For x1:=Low(img) To High(img) Do
  61.     For y1:=Low(img[x1]) To High(img[x1]) Do
  62.       Begin
  63.         Writeln(AName, ' Element ',x1,'x',y1,' = ',img[x1,y1]);
  64.       end;
  65. End;
  66. begin
  67.   DrawPicture(0,0,small_img,'small_img');
  68.   Writeln('------------------------------');
  69.   DrawPicture(0,0,laz_img,'laz_img');
  70.   ReadLn;
  71. end.
I used the "AName"-argument to demonstrate, which array is processed
Instead of the Writeln do your Scaling or whatever else you do there.
And you don't need the "temp"-var
« Last Edit: September 22, 2026, 04:45:07 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

lazarusprogrammer

  • New Member
  • *
  • Posts: 35
Re: Dynamic array as pointer
« Reply #11 on: September 22, 2026, 06:22:30 pm »
You misunderstand
Oh my god. It was this easy.

I didn't know that you can set an array without setting it's dimensions first.

Thank you very much Jamie and Zvoni.


creaothceann

  • Sr. Member
  • ****
  • Posts: 481
Re: Dynamic array as pointer
« Reply #12 on: September 22, 2026, 08:57:30 pm »
So the idea is to have an image you can use in a program (game) without needs of loading it from a file and ship it with a program.

For one image it's simple i could just hard code the image name to draw procedure. But for multiple different images it needs something else.

You could look into resources, or image lists, or even using hidden TImage instances.

With Unleashed Pascal you can embed files from the source code.

jamie

  • Hero Member
  • *****
  • Posts: 8071
Re: Dynamic array as pointer
« Reply #13 on: September 22, 2026, 11:35:51 pm »
So that is different than using ResourceString ?

Jamie
The only true wisdom is knowing you know nothing

Zvoni

  • Hero Member
  • *****
  • Posts: 3595
Re: Dynamic array as pointer
« Reply #14 on: September 23, 2026, 07:55:16 am »
You misunderstand
Oh my god. It was this easy.

I didn't know that you can set an array without setting it's dimensions first.

Thank you very much Jamie and Zvoni.
But only if you initialize it directly at declaration with Values

IF you don't initialize during declaration you have to go through SetLength et al.

btw: Just did a small test: You'd still be able to access a 2D-Array as a 1D-Array using a Pointer.
Memory-Layout is column-wise. You'd just need to keep track where in the 1D-Array you are (easy with simple math --> keyword "div/mod"-Operators)
Of course, this only works reliably for square 2D-Arrays. Jagged Arrays not so much
« Last Edit: September 23, 2026, 07:57:21 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018