Search code examples
adaabstract-data-type

Access the value using pointer in Ada


I am somewhat new in Ada and having some difficulties with the syntax of this language.

I've implemented a stack, and to push a value to it, for example, I need to use a function Push(Stack_instance, value).

I need many such stack instances, not of a fixed size. So I thought to use pointer to stack, which will create new stack object every time I need one.

Now, the problem I have is that after creating a pointer to stack that points to a new stack, how can I push a value to this stack instance? I can't use Push(Stack, value) since the Push function requires type Stack and I have Ptr_Stack. For example, in C, we have *ptr through which we can access value, but is there anything similar in Ada?


Solution

  • You can either do:

    Push (Stack.all, Value);
    

    If you are using Ada 2005 or more recent, and your stack type is a tagged type, you can also use the slightly more user-friendly:

    Stack.Push (Value);
    

    Finally, you can also change the declaration of Push to accept a pointer to a stack, as in:

    procedure Push (Stack : not null access Stack_Type; Value : ...);
    Push (Stack, Value);