Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

data movement using pointers in COBOL

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
Amb

New User


Joined: 12 Mar 2010
Posts: 64
Location: India

PostPosted: Tue Jul 13, 2010 10:06 am    Post subject: data movement using pointers in COBOL
Reply with quote

Hi Guys,

I have the below statements to load address of var11 and var22 to ptr-1 and ptr-2 respectively.

Set address of var11 to ptr-1.

Set address of var22 to ptr-2.


Now I need to move data in var11 to var22 using the ptr-1 and ptr-2.

Can you please help me with this as I am new with pointers and also not able to find much information on internet?
Back to top
View user's profile Send private message

Amb

New User


Joined: 12 Mar 2010
Posts: 64
Location: India

PostPosted: Tue Jul 13, 2010 11:27 am    Post subject:
Reply with quote

sorry guys, just one change I have used the below statements
to set the values for ptr-1 and ptr-2

Set ptr-1 to address of var11.

Set ptr-2 to address of var22.
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2502
Location: Atlanta, Georgia, USA

PostPosted: Tue Jul 13, 2010 4:03 pm    Post subject: Reply to: data movement using pointers in COBOL
Reply with quote

Using pointers and/or addresses, there isn't any data movement.

To point VAR22 at the data in VAR11 -

SET ADDRESS OF VAR22 TO PTR-1.

Actually, you can also issue -

SET ADDRESS OF VAR22 TO ADDRESS OF VAR11.

Which doesn't require a POINTER to be defined.

Are both VAR22 and VAR11 the same length?

If the length of VAR22 is greater than the length of VAR11, you will need reference modification to query the data.

IF VAR22 (1:LENGTH OF VAR11) =

Hopefully, your example is hypothetical, because you shouldn't mess with POINTERS/ADDRESSES unless you know what you're doing.

Bill
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 7914
Location: Bellevue, IA

PostPosted: Tue Jul 13, 2010 4:04 pm    Post subject:
Reply with quote

What does this mean?
Quote:
Now I need to move data in var11 to var22 using the ptr-1 and ptr-2.
COBOL does not move data using pointers -- COBOL moves data with the MOVE statement. Assuming both variables have addresses, of course.
Back to top
View user's profile Send private message
Kjeld

Active User


Joined: 15 Dec 2009
Posts: 365
Location: Denmark

PostPosted: Tue Jul 13, 2010 4:56 pm    Post subject:
Reply with quote

In a previous topic you asked for help in determining if your module had got addresses passed for 2 variables (var11 and var22).

Don't get mislead into thinking that you should work with pointers to manipulate these variables. The previous topic gave you an answer how to test if you had valid addressability established for these 2 variables. If the test is positive (not NULL) you can use the definitions in the linkage area as usual.

If the test is negative (NULL), you should not execute any references to these variables.

This is an example:
Code:

IF ADDRESS OF var11 = NULL
    MOVE ws-default-var11 TO ws-var11
  ELSE
    MOVE var11            TO ws-var11
END IF.
IF ADDRESS OF var22 = NULL
    MOVE ws-default-var22 TO ws-var22
  ELSE
    MOVE var22            TO ws-var22
END IF.

This way the rest of your code can execute without referencing the linkage section items. You should define the ws-* variables in your module's working storage section.
Back to top
View user's profile Send private message
Amb

New User


Joined: 12 Mar 2010
Posts: 64
Location: India

PostPosted: Wed Jul 14, 2010 6:56 am    Post subject: Reply to: data movement using pointers in COBOL
Reply with quote

Hi Guys,

Thanks for the information.

My reqirement was to move data from VAR11 to VAR22 using addresses of VAR11 and VAR22.
As suggested by Bill I am able to achieve that by setting the addresses using the below statement

Code:
SET ADDRESS OF VAR22 TO ADDRESS OF VAR11.



Just one more question:

After the above statement is executed VAR22 will contain same data as VAR11.
Let us say data type of VAR11 and VAR22 is PIC X(10).
I want VAR11(1:5) to be moved to VAR22(2:5).

How to I so this using the above SET address CODE?
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 7914
Location: Bellevue, IA

PostPosted: Wed Jul 14, 2010 7:45 am    Post subject:
Reply with quote

Quote:
My reqirement was to move data from VAR11 to VAR22 using addresses of VAR11 and VAR22.
As suggested by Bill I am able to achieve that by setting the addresses using the below statement
Statements like this prove you have no concept of what you are talking about and should not under any circumstances be attempting to use pointers or addresses in your code. You absolutely, categorically CANNOT MOVE DATA FROM VARIABLE TO VARIABLE BY CHANGING A VARIABLE'S ADDRESS! As further evidence, your question
Quote:
After the above statement is executed VAR22 will contain same data as VAR11.
Let us say data type of VAR11 and VAR22 is PIC X(10).
I want VAR11(1:5) to be moved to VAR22(2:5).
is meaningless in the sense that reference modification is a MOVE and you are not talking about MOVE -- you are talking about address changes. Your question is exactly the same as asking how the READ statement calculates a total -- totally without meaning.

Let us assume your program is loaded and ready to run with your var11 and var22 defined as PIC X(10). The variables in COBOL are (note that the first columns are not actually in the computer -- anywhere):
Code:
Variable     Memory location     Data at memory location 00DD0000
var11        00DD0000            QUICKBROWNNOWISTHETI
var22        00DD000A
So a DISPLAY of var11 would print QUICKBROWN and a DISPLAY of var22 would print NOWISTHETI. Your code executes your SET statement. What does the memory look like now?
Code:
var11        00DD0000            QUICKBROWNNOWISTHETI
var22        00DD0000
A DISPLAY of var11 would print QUICKBROWN and a DISPLAY of var22 would print QUICKBROWN. Note that the "old" data in var22 is still in memory, still in the same spot in memory -- but now you have no way to access it; in Java or C those 10 bytes would become eligible for garbage collection. Since COBOL doesn't use dynamic structures, there is no garbage collection and those bytes would retain their values until the program completes.
Back to top
View user's profile Send private message
Amb

New User


Joined: 12 Mar 2010
Posts: 64
Location: India

PostPosted: Wed Jul 14, 2010 8:25 am    Post subject:
Reply with quote

hi robert,

I think I should not have used the word "MOVE data"... and rather used the word "POINT data"

as suggested by you after the statement

Code:
SET ADDRESS OF VAR22 TO ADDRESS OF VAR11


is executed

Quote:
A DISPLAY of var11 would print QUICKBROWN and a DISPLAY of var22 would print QUICKBROWN


Can you please suggest me a way wherein VAR22 would point to UICKBRO rather than QUICKBROWN?

I hope I am clear with my question now.
Back to top
View user's profile Send private message
dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Wed Jul 14, 2010 9:02 am    Post subject:
Reply with quote

Hello,

Quote:
My reqirement was to move data from VAR11 to VAR22 using addresses of VAR11 and VAR22.
This is no requirement. . . It is also not even a good approach. . .

Quote:
Can you please suggest me a way wherein VAR22 would point to UICKBRO rather than QUICKBROWN?
As Robert has mentioned - you have no understanding of what is going on. . . A "pointer" is just that - a pointer. It points somewhere - a single somewhere - a single address. You are trying to get it to point to 2 places.

I'm not sure i actually understand what you are trying to do, but i have been "in the code" of tens of thousands of modules and no one has ever had this as a requirement.

It sounds like someone heard that pointers can be used in certain instances and the search was on to find a "requirement" that needs this. In other words - a solution in search of a problem to solve.

What are you trying to do that cannot be done with Reference Modification (which Bill suggested in the very first reply) icon_confused.gif
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 7914
Location: Bellevue, IA

PostPosted: Wed Jul 14, 2010 4:04 pm    Post subject:
Reply with quote

Quote:
Can you please suggest me a way wherein VAR22 would point to UICKBRO rather than QUICKBROWN?
This cannot be done. VAR22 does not point to QUICKBROWN -- the byte pointed to by the address for VAR22 is Q -- because an address is a 4-byte field that points to a single byte of memory. You could use a COBOL MOVE statement to move the UICKBRO bytes to another variable, but not by using pointers or SET ADDRESS. Pointers and SET ADDRESS do not allow you to change the variable length so you get all 10 bytes (per your example) of the variable every time.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Wed Jul 14, 2010 4:58 pm    Post subject:
Reply with quote

something to think about:
Code:

LINKAGE-SECTION.
  01  LK-VAR-11.
        05  FIELD-1       PIC X(10).
        05  FIELD-2       PIC X(10).

  01  LK-VAR-22.
        05  FIELD-1       PIC X(10).
        05  FIELD-2       PIC X(10).

  01  LK-VAR-33.
        05  FILLER        PIC X(05).
        05  FIELD-1A      PIC X(05).

PROCEDURE DIVISION USING LK-VAR-11.

SET ADDRESS OF LK-VAR-22 TO ADDRESS OF LK-VAR-11.
SET ADDRESS OF LK-VAR-33 TO ADDRESS OF LK-VAR-22.

MOVE FIELD-1A   TO WS-VAR-1


in the above scenario, if LK-VAR-11 contained
'12345AAAAA67890BBBBB'.
and ws-var-1 was defined as PIC X(05),
then WS-VAR-1 would contain AAAAA after the move.

once the above is understood, possibly this thread can come to an end.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Wed Jul 14, 2010 5:04 pm    Post subject:
Reply with quote

Code:

WORKING-STORAGE.

  01  POINTERS-AND-OTHER-STUFF.
        05  SILLY-POINTER    USAGE IS POINTER.
        05  EVEN-MORE-BS   
             REDFINES
             SILLY-POINTER     PIC S9(08) COMP.

LINKAGE-SECTION.
  01  LK-VAR-11.
        05  FIELD-1       PIC X(10).
        05  FIELD-2       PIC X(10).

  01  LK-VAR-33.
        05  FIELD-1A      PIC X(05).

PROCEDURE DIVISION USING LK-VAR-11.

SET SILLY-POINTER TO ADDRESS OF LK-VAR-11
ADD 5 TO EVEN-MORE-BS
SET ADDRESS OF LK-VAR-33  TO SILLY-POINTER
MOVE FIELD-1A   TO WS-VAR-1


in the above scenario, if LK-VAR-11 contained
'12345AAAAA67890BBBBB'.
and ws-var-1 was defined as PIC X(05),
then WS-VAR-1 would contain AAAAA after the move.
Back to top
View user's profile Send private message
Kjeld

Active User


Joined: 15 Dec 2009
Posts: 365
Location: Denmark

PostPosted: Wed Jul 14, 2010 5:07 pm    Post subject:
Reply with quote

In Cobol, the REDEFINES construct is used to address the same storage with different variable definitions.

I suggest you explore the possibilities of redefining your storage before venturing into pointer gymnastics.

Quote:
After the above statement is executed VAR22 will contain same data as VAR11.
Let us say data type of VAR11 and VAR22 is PIC X(10).
I want VAR11(1:5) to be moved to VAR22(2:5).

How to I so this using the above SET address CODE?

Your additional question is not clear: Your source string (VAR11(1:5) (5 bytes) is different length than your destination string VAR22(2:5) (4 bytes)!
If your requirement is to shift the first 5 bytes of the string 1 byte, you can write:
Code:
MOVE VAR11(1:5) to VAR11(2:6) or
MOVE VAR11(1:5) to VAR22(2:6)

In both cases your storage would then contain 'QQUICKROWN', assumed the starting adress of the two variables were the same.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
This topic is locked: you cannot edit posts or make replies. Unwrap the data based on delimiter X'25' bhavana yalavarthi DFSORT/ICETOOL 30 Fri Dec 09, 2016 10:25 am
No new posts Is the 'prompt' data for the DSLIST p... Willy Jensen TSO/ISPF 2 Tue Dec 06, 2016 4:38 am
This topic is locked: you cannot edit posts or make replies. How to move a long alphanumeric data ... lind sh COBOL Programming 8 Mon Dec 05, 2016 7:51 pm
This topic is locked: you cannot edit posts or make replies. RANDOM Function in COBOL swapnil781 COBOL Programming 2 Tue Nov 15, 2016 6:17 pm
No new posts ODPP(Optim Data privacy Provider) Iss... Rama kishore IBM Tools 1 Mon Nov 07, 2016 5:46 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us