resolving circular reference lazarus / 62-General ---------------------------------------------------------------------- 440bx · 2022-03-07 07:35 Hello, I've run into circular references in the translation of C functions and structures.  The sample below shows the problem using an abbreviated version of the structures and functions involved:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$APPTYPE CONSOLE} program _CircularPointers; type  { _DDHAL_DESTROYDRIVERDATA                                                }   PDDHAL_DESTROYDRIVERDATA = ^TDDHAL_DESTROYDRIVERDATA;  TDDHAL_DESTROYDRIVERDATA = record    lpDD                       : PDDRAWI_DIRECTDRAW_GBL;  { defined below!    }     { more fields                                                           }  end;   { _DDHAL_DESTROYDRIVER                                                    }   PDDHAL_DESTROYDRIVER = function                           (                          { _in_ } InDriver : PDDHAL_DESTROYDRIVERDATA                         )                         : DWORD; stdcall;   { _DDHAL_DDCALLBACKS                                                      }   PDDHAL_DDCALLBACKS = ^TDDHAL_DDCALLBACKS;  TDDHAL_DDCALLBACKS = record    dwSize               : DWORD;    dwFlags              : DWORD;    DestroyDriver        : PDDHAL_DESTROYDRIVER;  end;   { _DDHAL_CALLBACKS                                                        }   PDDHAL_CALLBACKS = ^TDDHAL_CALLBACKS;  TDDHAL_CALLBACKS = record    cbDDCallbacks              : TDDHAL_DDCALLBACKS;  end;   { _DDRAWI_DIRECTDRAW_GBL                                                  }   PDDRAWI_DIRECTDRAW_GBL = ^TDDRAWI_DIRECTDRAW_GBL;  TDDRAWI_DIRECTDRAW_GBL = record    lpDDCBtmp                : PDDHAL_CALLBACKS;     { many more fields                                                      }  end; beginend.  The problem is there is a circular reference to PDDRAWI_DIRECTDRAW_GBL.  As a diagram ( "->" means "references")   PDDRAWI_DIRECTDRAW_GBL                           <- circular reference     -> PDDHAL_CALLBACKS                             |          -> TDDHAL_DDCALLBACKS                      |               -> PDDHAL_DESTROYDRIVER               |                    -> PDDHAL_DESTROYDRIVERDATA      |                         -> PDDRAWI_DIRECTDRAW_GBL  -^ circular reference The only solution I can think of so far is to change the parameter of the PDDHAL_DESTROYDRIVER function to be a plain "pointer" and move the definition of P/TDDHAL_DESTROYDRIVERDATA to be below the definition of PDDRAWI_DIRECTDRAW_GBL.  That solution is undesirable because the parameter of PDDHAL_DESTROYDRIVER becomes a plain pointer instead of a pointer to a specific structure. My question is: is there a way in Pascal to declare these structures without losing any information (such as the parameter type of PDDHAL_DESTROYDRIVER) ? Thank you for your help. ---------------------------------------------------------------------- Zvoni · 2022-03-07 08:47 WorksCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; type  PFirstRecord = ^TFirstRecord;   PSecondRecord = ^TSecondRecord;  TSecondRecord = Record    FirstField  : Integer;    SecondField : String;    ThirdField  : PFirstRecord;  end;   PThirdRecord = ^TThirdRecord;  TThirdRecord = Record    Field1     : Integer;    Field2     : PSecondRecord;  end;   PFourthRecord = ^TFourthRecord;  TFourthRecord = Record    Field100    : PThirdRecord;  end;   TFirstRecord = Record    Field1000  : PSecondRecord;  end;  beginend. ---------------------------------------------------------------------- marcov · 2022-03-07 08:47 Only reference types can be forwardly declared in Pascal (and only within one single TYPE block), but in this case that is enough. Move all pointer definitions to the top and it will compile. ---------------------------------------------------------------------- 440bx · 2022-03-07 15:09 @Zvoni and Marcov, Thank you both for your help. @marcov, you are right.  Declaring all the pointers first solved the problem.  I should have thought of that but, obviously I didn't.