Hello Friends,
Pointers are one of the key features in AS400 RPG programming. If you
are a beginner and just started to write some RPG code then it is good to
understand the concept of pointers.
What is pointer?
Pointers are used to hold the memory address of a variable (just like
how it used in C). This will become handy when we need to process larger size
data.
Let us take a simple example:
Pointer can be declared using asterisk (*) symbol. We can assign this
pointer to a variable using Based
keyword. In the below example P_Data
is based on pointer Ptr. This means,
Ptr will hold any address and P_Data will display the content of that address.
We can see from above that, address of Var1 is assigned to Ptr. Now Var1
& P_Data points to same address. So if we change Var1 data, it will be
reflected in P_Data.
Similarly, if we change P_Data value then it will be reflected in Var1
value.
Segmentation with pointers:
Using pointers we can split the large data into small chunks and access
it.
Let us take below example:
Here Var1 is of length 100 but we are accessing the data by splitting
into small chunks (of 10 length) using P_Data variable.
We initially assign address of Var1 into Ptr and then we increment the
Ptr value by adding size of P_Data.
Output:
Do you know this trick?
Hopefully you might have used RPG calling another RPG using parameter. We
would have noticed same variable used for both input & output. (i.e. if PGMA
calls PGMB has parameter ABC, then I change the value of ABC in PGMB then it
will reflect in PGMA automatically).
This is because; the two-way parameter is achieved using "shared
memory".
When one program calls another, the only thing that’s passed between
them is an address in the computer’s memory where the parameter starts. Nothing
else is passed.
Look at the below example for better understanding.
TESTPGM à calls GETNAME it passes Name as parameter. But length of Name is
10 in TESTPGM. But we receive this value using *Entry Plist under FullName
variable (of length 20) in GETNAME program. We assigned value “Mohammed Yusuf”
and program returned the value to calling program (TESTPGM).
Now, if we display variable Name we get “Mohammed Y” alone. The remaining
value gets assigned to Address variable (since both are under DS)
Output:
Name = “Mohammed Y”
Address = “usuf”
What happened here? When program “GETNAME” called, system passed the
address of Name variable. And GETNAME program assigned the value “Mohammed
Yusuf” in that address.
Now when TESTPGM receives it, Name variable can hold only 10 length so
remaining text is present in Address variable. (because Name & Address are
in single Data Structure so it forms continues memory allocation)
How to overcome this?
Instead of *Entry Plist, start using PI (Procedure Interface)
- It is similar to *Entry Plist (but better than that)
- It requires matching prototype to work
- Replacement of *Entry Plist in Free format
The program GENAME is changed to GETNAMEPR and uses PI to receive the
parameter. The calling program TEST is changed with Prototype definition of
GETNAMEPR.
With this in hand, if we try to call GETNAMEPR with variable Name it will throw error since length of
Name is not matching with parameter length
of Prototype.
Parameters & Prototype has many useful things in ILE programming. I
will try to cover those in my upcoming posts.
Until then, Have fun…!!! Happy coding…!!!
No comments:
Post a Comment