Oso wrote: ↑Thu Nov 24, 2022 10:36 pm
...
But could you do this without the structure? In other words, can you just write back the result to *Width for example?
...
Because you can't use typed pointers with the basic types
...
I don't follow the above sentence. I tended to think pointers were just that — a pointer to a memory location, which is always going to be a number?
Hi Oso,
because you cannot code this with the newer compilers:
Code: Select all
Procedure GetSize_WithCompilerErrorMessage(*Width.i, *Height.i)
; Pointer with Native Type is not possible!
; ==> Compiler Error Message: "Native types can't be used with pointers."
Here is an extended example.
Code: Select all
EnableExplicit
Procedure GetSize(*Width.INTEGER, *Height.INTEGER)
; some kind of useless code to demonstrate the Parameter By Ref
Debug #PB_Compiler_Procedure + "() => *W, *H = " + *Width + ", " + *Height + " -- *W\i, *H\i = " + *Width\i + ", " + *Height\i
*Width\i = 800
*Height\i = 600
Debug Space(Len(#PB_Compiler_Procedure)) + " => *W, *H = " + *Width + ", " + *Height + " -- *W\i, *H\i = " + *Width\i + ", " + *Height\i
ProcedureReturn #True
EndProcedure
Procedure GetSize_differently(*Width, *Height)
; some kind of useless code to demonstrate the Parameter By Ref
Debug #PB_Compiler_Procedure + "() => *W, *H = " + *Width + ", " + *Height
*Width = 800
*Height = 600
Debug Space(Len(#PB_Compiler_Procedure)) + " => *W, *H = " + *Width + ", " + *Height
ProcedureReturn #True
EndProcedure
; Test
Global w, h
Debug ""
If GetSize(@w, @h)
Debug " set to " + w + ", " + h
EndIf
Debug ""
If GetSize_differently(@w, @h)
Debug " set to " + w + ", " + h
EndIf
; >> Debug Output << Window
;
; GetSize() => *W, *H = 5368975752, 5368975744 -- *W\i, *H\i = 0, 0
; => *W, *H = 5368975752, 5368975744 -- *W\i, *H\i = 800, 600
; set to 800, 600
;
; GetSize_differently() => *W, *H = 5368975752, 5368975744
; => *W, *H = 800, 600
; set to 800, 600
;
As you can see, with integer variables you can work with pointers to get the same result.
However, you change the address to the memory location and not the content of the memory location. (see the Debug output)
IMHO it's more correct (more beautiful, better, more professional, etc.) to do it like the first way!
How it looks with the other native data types, you can check yourself by extending the example.
Mostly running PureBasic <latest stable version and current alpha/beta> (x64) on Windows 11 Home