Recent

Author Topic: Address of function in typeless pointer constant  (Read 1259 times)

TCH

  • Full Member
  • ***
  • Posts: 200
Address of function in typeless pointer constant
« on: October 14, 2021, 11:00:33 pm »
I have a small program:
Code: Pascal  [Select][+][-]
  1. program x;
  2.  
  3. function kecske(a: integer): integer; cdecl;
  4. begin
  5.         kecske := a + 8;
  6. end;
  7.  
  8. var ptr: Pointer = Pointer(@kecske);
  9.  
  10. begin
  11. end.
fpc x.pas
Code: [Select]
x.pas(8,36) Error: Illegal expressionIf i change the var to const, the result is identical. How can i store a function's address to a constant or an initialized variable as an untyped pointer?

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Address of function in typeless pointer constant
« Reply #1 on: October 15, 2021, 12:59:02 am »
Code: Pascal  [Select][+][-]
  1. program pointerEx;
  2.  
  3. {$mode objfpc}
  4.  
  5. function kecske(a: integer): integer; cdecl;
  6. begin
  7.   kecske := a + 8;
  8. end;
  9.  
  10. var
  11.   pKecske: Pointer = @kecske;
  12.  
  13. begin
  14. end.
This compiles OK for me. 

TCH

  • Full Member
  • ***
  • Posts: 200
Re: Address of function in typeless pointer constant
« Reply #2 on: October 15, 2021, 10:09:09 am »
Yeah, that worked for me too, but unfortunately it did not worked in my real case. My example program was incomplete. This was more like what i wanted to accomplish:
Code: Pascal  [Select][+][-]
  1. program x;
  2.  
  3. type
  4.         TProc = procedure; cdecl;
  5.         TKecske = function (a: integer): integer; cdecl;
  6.         PPointer = ^Pointer;
  7.  
  8. var Func: TProc;
  9.  
  10. function kecske(a: integer): integer; cdecl;
  11. begin
  12.         kecske := a + 8;
  13. end;
  14.  
  15. const
  16.         pKecske: Pointer = @kecske;
  17.         pFunc: Pointer = @Func;
  18.  
  19. procedure set_it1(src: Pointer; dst: PPointer);
  20. begin
  21.         dst^ := src;
  22. end;
  23.  
  24. begin
  25.         set_it1(pKecske, @pFunc);
  26.         WriteLn(TKecske(pFunc)(8));
  27. end.
But in the meantime, i figured it out, the catch was casting the pointer to ppointer. Thanks for helping.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Address of function in typeless pointer constant
« Reply #3 on: October 15, 2021, 12:11:18 pm »
I think it is generally better practice to use typed pointers, or if you have to use an untyped pointer cast it to the correct type:

Code: Pascal  [Select][+][-]
  1. program pointerEx;
  2.  
  3. {$mode objfpc}
  4.  
  5. type
  6.  
  7.   TFunc = function(b: Integer): Integer; cdecl;
  8.  
  9. function kecske(a: integer): integer; cdecl;
  10. begin
  11.   kecske := a + 8;
  12. end;
  13.  
  14. var
  15.   pKecske: Pointer = @kecske;
  16.  
  17. begin
  18.   WriteLn(TFunc(pKecske)(8);
  19.   ReadLn;
  20. end.
             

TCH

  • Full Member
  • ***
  • Posts: 200
Re: Address of function in typeless pointer constant
« Reply #4 on: October 15, 2021, 12:25:12 pm »
I have to work with different type of procedures and functions and store their addresses in a common list, so i only can store them as untyped pointers; usage is done with casting of course.

 

TinyPortal © 2005-2018