Pascal pointer to pointer SyntaxError and Incompat

2019-09-20 16:02发布

问题:

Since there's no way to create refereces (is there?) I have to do it with a pointer to another pointer. This gives me an error:

type
  PNode = ^TNode;
  TNode = record
    char: char;
    next: PNode;
    children: PNode;
  end;

  PPNode = ^PNode; 
var
    current_node: PPNode;  
function find_or_insert_peer(var node: PNode; character: char): PNode; 

current_node := @find_or_insert_peer(current_node^, character)^.children;

x.pas(121,23) Error: Incompatible types: got "<address of function(var PNode;Char):^TNode;Register>" expected "PPNode"
x.pas(121,43) Fatal: Syntax error, ";" expected but "(" found
Fatal: Compilation aborted

I also tried this but it does not compile because of SyntaxError

current_node := @(find_or_insert_peer(current_node^, character)^.children);

Edit

Please help. I've even created a demo in c++ to show you this is legal. http://cpp.sh/8mxe

回答1:

As mentioned in a witty-called article "Addressing pointers" linked in comments the @ operator might not work properly with more complex expressions. Its equivalent addr() will do the trick.

current_node := addr(find_or_insert_peer(current_node^, character)^.children);


标签: pascal