Static glass Delphi



  • Why isn't anything going on at the Push?

    type
      TStack = record
      StackArray : array [1..5] of Integer;
      top : Integer;
    end;
    

    var MyStack : TStack;
    i,a,x : Integer;
    procedure Push(s : TStack;a : Integer);
    begin
    s.top := s.top + 1;
    s.StackArray[s.top] := a;
    end;
    procedure Pop(s : TStack);
    begin
    s.top := s.top - 1;
    end;

    function TTop(s : TStack) : Integer;
    begin
    TTop := s.StackArray[s.top];
    end;

    begin
    MyStack.top := 0;

    Push(MyStack,23);
    Write('TOP : ');
    writeln(MyStack.top);

    Readln;

    end.



  • Delphi record--- it's a matter, not a reference. That is, a local copy is being created within the procedure, and therefore a change in the parameter s internal procedure Push There's no impact on the state. MyStack

    To get what you want, add. var parameter s:

    procedure Push(var s : TStack;a : Integer);
    begin
      s.top := s.top + 1;
      s.StackArray[s.top] := a;
    end;
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2