Menu

Passing pointers question ...

2017-11-27
2017-11-28
  • Gregory A Failing

    I am sure that I have a misunderstanding about how to use SET ADDRESS OF. In the following example I wish to pass a pointer of a field in the calling program to a called program so the called program will load a value into the area pointed to by the pointer value ie:

           IDENTIFICATION DIVISION.
           PROGRAM-ID. example1.
           ENVIRONMENT DIVISION.
           CONFIGURATION SECTION.
           SOURCE-COMPUTER.    UNIX.
           OBJECT-COMPUTER.    UNIX.
          *
           DATA DIVISION.
           WORKING-STORAGE SECTION.
           01  FILLER.
               03 FILLER         PIC X(016)     VALUE 'FCSI CodeWerks:'.
               03 FILLER         PIC X(064)     VALUE
                  'Name:example1.cbl  Version:5.7.1  Date:2017-11-26'.
               03 FILLER         PIC X(002)     VALUE LOW-VALUES.
          *
           01  FIELD1            PIC 9(004)     VALUE ZEROES.
          *
           01  FIELD2            PIC X(032)     VALUE ALL 'Z'.
          *
           01  FIELD3            PIC 9(012)     VALUE ZEROES.
          *
          *
           01  CALL-AREA.
               03 CA-KEYTYPE     PIC X(009).
          * Set to maximum of chars to return; returns actual char count
               03 CA-MAXCHAR     PIC 9(004).
          * Set this pointer to the receiving area for the value string.
               03 CA-POINTER     USAGE POINTER.
          *
          /
           PROCEDURE DIVISION.
           MAIN-CODE SECTION.
           MAIN-CODE-0000.
               DISPLAY ' ' LINE 1 POSITION 1.
    
               MOVE 'FIELD1' TO CA-KEYTYPE.
               MOVE ZEROES TO CA-MAXCHAR.
               INSPECT FIELD1 TALLYING CA-MAXCHAR FOR CHARACTERS.
               SET CA-POINTER TO ADDRESS OF FIELD1.
               CALL 'gafgetdata' USING CALL-AREA.
               DISPLAY "Field1=" LINE 01 COL 01,
                       FIELD1(1:CA-MAXCHAR) LINE 00 COL 00.
    
               MOVE 'FIELD2' TO CA-KEYTYPE.
               MOVE ZEROES TO CA-MAXCHAR.
               INSPECT FIELD2 TALLYING CA-MAXCHAR FOR CHARACTERS.
               SET CA-POINTER TO ADDRESS OF FIELD2.
               CALL 'gafgetdata' USING CALL-AREA.
               DISPLAY "Field3=" LINE 03 COL 01,
                       FIELD2(1:CA-MAXCHAR) LINE 00 COL 00.
    
               MOVE 'FIELD3' TO CA-KEYTYPE.
               MOVE ZEROES TO CA-MAXCHAR.
               INSPECT FIELD3 TALLYING CA-MAXCHAR FOR CHARACTERS.
               SET CA-POINTER TO ADDRESS OF FIELD3.
               CALL 'gafgetdata' USING CALL-AREA.
               DISPLAY "Field3=" LINE 05 COL 01,
                       FIELD3(1:CA-MAXCHAR) LINE 00 COL 00.
    
           MAIN-CODE-EXIT.
               STOP RUN.
          * *
          *
          *================================================================*
           IDENTIFICATION DIVISION.
           PROGRAM-ID. gafgetdata.
           ENVIRONMENT DIVISION.
           DATA DIVISION.
           WORKING-STORAGE SECTION.
          *
           01  TEXT-AREA              PIC X(256) based.
    
          /
           LINKAGE SECTION.
          *
           01  L-CALL-AREA.
               03 CA-KEYTYPE     PIC X(009).
               03 CA-MAXCHAR     PIC 9(004).
               03 CA-POINTER     USAGE POINTER.
          /
           PROCEDURE DIVISION USING L-CALL-AREA.
          *
           MAIN-CODE SECTION.
           MAIN-CODE-0000.
               EVALUATE CA-KEYTYPE
                   WHEN 'FIELD1'
                       SET address of TEXT-AREA TO address of CA-POINTER
                       MOVE 4 TO CA-MAXCHAR
                       MOVE '0001' TO TEXT-AREA(1:CA-MAXCHAR)
                   WHEN 'FIELD2'
                       SET address of TEXT-AREA TO address of CA-POINTER
                       MOVE 8 TO CA-MAXCHAR
                       MOVE 'GNUCobol' TO TEXT-AREA(1:CA-MAXCHAR)
                   WHEN 'FIELD3'
                       SET address of TEXT-AREA TO address of CA-POINTER
                       MOVE 2 TO CA-MAXCHAR
                       MOVE '12' TO TEXT-AREA(1:CA-MAXCHAR)
               END-EVALUATE.
           MAIN-CODE-EXIT.
               GOBACK.
          *
           END PROGRAM gafgetdata.
          *
           END PROGRAM example1.
    

    Expected display:

    Field1=0001
    
    Field3=GNUCobol
    
    Field3=12
    

    Observed display:

    Field1=0000
    
    Field3=ZZZZZZZZ
    
    Field3=00
    

    Obviously the values set in the calling program are not getting established by the called program.

    What would be the correct way to do this?

    Thank you for your comments and help.

    Gregory

     

    Last edit: Simon Sobisch 2017-11-27
    • Patrick McCavery

      Hi Greg

      Brian rescued me with this one last spring:
      https://sourceforge.net/p/open-cobol/discussion/help/thread/d7a52a60/

      It looks like the first 3 pointer changes are missing the "set address of"

      -Pat

      Mod-Edit: removed reply-to

       

      Last edit: Simon Sobisch 2017-11-27
      • Gregory A Failing

        Hi Pat ...

        Regarding " It looks like the first 3 pointer changes are missing the "set address of" ..."

        I'm not seeing what you are referring to ... the called program (gafgetdata) is doing what you are suggesting ie:

                       SET address of TEXT-AREA TO address of CA-POINTER
        

        Gregory

        Mod-Edit: removed reply-to

         

        Last edit: Simon Sobisch 2017-11-27
        • PatMcC

          PatMcC - 2017-11-27

          Hi Greg

          I have a million things coming at me at once. I am probably not sane.

          What is the program doing or not doing? You seem to know the right
          syntax already, is it not working?

          -pat

           
          • Gregory A Failing

            No worries Pat ... Simon explained the semantics and suggested the correct syntax.

            G

             
  • Simon Sobisch

    Simon Sobisch - 2017-11-27

    Gregory, you did the following

    SET CA-POINTER TO ADDRESS OF FIELD1.
    *> the value of CA-POINTER in your prog contains the address of FIELD1 
    
    CALL ...
    *> the value of CA-POINTER in gafgetdata contains the address of FIELD1
    
    SET address of TEXT-AREA TO address of CA-POINTER
    *> TEXT-AREA contains the address of CA-POINTER
    

    You likely wanted

    SET address of TEXT-AREA TO CA-POINTER
    *> TEXT-AREA is set to the value of CA-POINTER which is the address of FIELD1
    

    Quick check:

    Field1=0001
    
    Field3=GNUCobol
    
    Field3=12
    end of program, please press a key to exit
    

    note: the program misses auto correction to get to the real GnuCOBOL ;-)

     

    Last edit: Simon Sobisch 2017-11-27
    • Gregory A Failing

      Simon,

      You are exactly correct. I must presume that I was relating to 'c' and ignoring the implied symantics in the docs. I knew some combination would work and I was sure that I tried your suggestion but then again maybe not.

      Thanks for your help.

      PS: there are a few 'address of' examples in the GNU Cobol FAQ but they don't address this style of usage very well if at all. It would be useful for common error message routines, commandline argument processing, and other library functions. Feel free to use these programs (with appropriate mods) as an added example if needed.

       
      • Simon Sobisch

        Simon Sobisch - 2017-11-28

        There's a separate thread for feedback about the FAQ at [c67ea739], feel free to post a request there.
        Also check the Programmer's Guide, if you find something is wrong or missing post a new Wish-List item or a new Bug with the milestone "Programmer's Guide".

         

        Related

        Discussion: c67ea739


        Last edit: Simon Sobisch 2017-11-28

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.