# Thread: TechTip: RPG IV Pointers--They're Easy!

1. Guest.Visitor Guest

## TechTip: RPG IV Pointers--They're Easy!

** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
I've always understood how to use pointers I'm just not clear on what the advantage is to using them.

2. Junior Member
Join Date
Dec 1969
Posts
4

## TechTip: RPG IV Pointers--They're Easy!

** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
Hello Bill, One recent example is to be able to call a procedure that will fill an external data structure with data and by simply passing a pointer to the data sturcture you can access the data in your calling program, or pass the data to other programs/procedure without having to pass the entire data structure. Just be careful because the data in the data structure can be manipulated in any program/procedure it is passed to. This technique can be used to separate the data retrieval logic from the business logic. Mike
Code

3. Junior Member
Join Date
Dec 1969
Posts
4

## TechTip: RPG IV Pointers--They're Easy!

** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
Hi to all. This is my question (about pointers): I've 3 vars: (Numeric) Num1 = 100 Num2 = 200 Num3= 300 Idx = 1 Result = *zeros I've 1 var: (Char 10) NamVar = 'Num' + %char(Idx) = 'Num1' ok, now, how can I retrieve a var Result throws NamVar? I want: Result = (something)NamVar = 100 Is it possible in RPG? Thanks. Victor.

4. ## TechTip: RPG IV Pointers--They're Easy!

** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **

5. lenstoner3 Guest

## TechTip: RPG IV Pointers--They're Easy!

** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
Thanks for the article Jim. I use pointers frequently to pass array addresses to various procedures which manipulate the array for use in the calling module. However, I've never really understood why I would need to use a procedure pointer. Do you have a really good example of this? Thanks.

6. Junior Member
Join Date
Dec 1969
Posts
13

## TechTip: RPG IV Pointers--They're Easy!

** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
Jeff, You are entirely correct! I use %len so much that I mistakenly used it instead of %size, as I should have. Good catch, and thanks for the correction. It's good to know that some people actually try out these tips. Jim

7. Junior Member
Join Date
Dec 1969
Posts
13

## TechTip: RPG IV Pointers--They're Easy!

** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
Victor, I have two responses for your inquiry. My first response is to do the following: Instead of using fields that have a number suffix, instead define an array (call it Num), with as many elements as you need. Load the values into the array using whatever process fits - maybe a loop, maybe not. When you need to get Result, use the assignment statement Result = Num(Idx); In this solution there is no need for NamVar. If your circumstances do not permit this solution, then consider my second solution coded and documented below. It's a longer solution. To answer your question specifically, there is no way to get the contents of a field, at run time, from its name. That is why I pre-load field addresses in the second solution. I hope you can use one of these two alternatives. Jim
Code

8. Guest.Visitor Guest

## TechTip: RPG IV Pointers--They're Easy!

** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
You can certainly use SORTA on a dynamically allocated array on V5R3. sorta %subarr(MyArray:Start:End) ; Chris

9. Senior Member
Join Date
Jun 2005
Posts
246

## TechTip: RPG IV Pointers--They're Easy!

** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
Jim: The pointer data type and based variables is another item I was responsible for implementing back in V3R1. For that sin, I have elsewhere offered my sincerest apologies. But then again, if I didn't implement it, someone else would have. There are a couple of problems with pointers as implemented in RPG. The most egregious is that they're untyped. That is, the compiler can't do any verification at all with respect to the data types involved in any pointer operation. Compare this with C. If you declare a pointer variable as say "int *p;", then the operation "p=&x;" is valid only if "x" is defined as "int". In RPG, a pointer can be assigned the address of anything, no matter what the type. RPG will quite happily let you do things that may well cause you hours of debugging. C will let you know of the problem at compile time. My recommendation is that if you think you have to use pointers in your RPG programming, use C instead. Cheers! Hans

10. Junior Member
Join Date
Dec 1969
Posts
13

## TechTip: RPG IV Pointers--They're Easy!

** This thread discusses the article: TechTip: RPG IV Pointers--They're Easy! **
I requested that MC Press fix my goof, using %len instead of %size, so it should be fixed now. Jim