IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Is it possible to redefine the comp variable


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
ismail.hasan

New User


Joined: 28 Mar 2007
Posts: 21
Location: India

PostPosted: Tue Apr 24, 2007 6:17 pm
Reply with quote

Hi,

I have 2 doubts.

1) is it possible to redefine the comp variable (p.s. its not comp-3 its comp)?

If so is it possible to redifine with cross datatypes
For e.g.

01 VAR1 PIC S9(06) comp
02 VAR2 REDEFINES VAR1
03 VAR3 PIC X(02)
03 VAR4 PIC s9(04) comp

2) is it possible to redifne the X variable with comp variable i.e

01 var1 pic x(04)
02 var2 redifnes var1
03 var3 pic s9(02) comp

Please let me whether above things are possible and how the mem location is allocated..?

Thanks in advance
Noor Mohammed Ismail
Back to top
View user's profile Send private message
Arun Raj

Moderator


Joined: 17 Oct 2006
Posts: 2481
Location: @my desk

PostPosted: Tue Apr 24, 2007 6:34 pm
Reply with quote

Hi Noor Mohammed Ismail

It is possible to redefine a COMP data-item with an alphanumeric data-item and vice-versa provided the level-nos of both the redefined and the redefining data-items are same.

Thanks
Arun
Back to top
View user's profile Send private message
ismail.hasan

New User


Joined: 28 Mar 2007
Posts: 21
Location: India

PostPosted: Tue Apr 24, 2007 6:42 pm
Reply with quote

hi arcvns,
Thanks for your reply.
My understanding from your reply is the way wat i have declared is not possible and if the pic is placed 02 level then it will work.
is my understanding is write? if not please educate in till bit ellabrate, if you dont mind.

If my understanding is write how the mem allocation will be for the second case the x(04) redifined by s9(02) comp. where s9(02) comp occupies only 2 byte and what happens to remaining 2 byte. will it through an error or it will be ingored.

Please help me
Back to top
View user's profile Send private message
Arun Raj

Moderator


Joined: 17 Oct 2006
Posts: 2481
Location: @my desk

PostPosted: Tue Apr 24, 2007 7:30 pm
Reply with quote

Hi

Code:
01 A PIC S9(6) COMP.

01 B REDEFINES A PIC X(2)


Code:
01 A PIC X(2).

01 B REDEFINES A PIC S9(6) COMP


if the redefining data-item is having a shorter length, the original data will get truncated and it wont give any error

Thanks
Arun
Back to top
View user's profile Send private message
Phrzby Phil

Senior Member


Joined: 31 Oct 2006
Posts: 1042
Location: Richmond, Virginia

PostPosted: Tue Apr 24, 2007 7:38 pm
Reply with quote

Quote:
01 VAR1 PIC S9(06) comp
02 VAR2 REDEFINES VAR1
03 VAR3 PIC X(02)
03 VAR4 PIC s9(04) comp


has a problem.

1. S9(6) COMP allocates a fullword - 4 bytes, and is pure binary, so a negative value will have the leftmost bit = 1, using complement arithmetic.
2. S9(4) COMP allocates a halfword - 2 bytes, with the same comments re binary.
3. Therefore, your redefines may be shifting VAR4 to the right OK, but its sign bit will not be aligned with VAR1's sign bit.
Back to top
View user's profile Send private message
ismail.hasan

New User


Joined: 28 Mar 2007
Posts: 21
Location: India

PostPosted: Tue Apr 24, 2007 7:40 pm
Reply with quote

Thanks lot arcvns. Very kind of u icon_smile.gif
Back to top
View user's profile Send private message
ismail.hasan

New User


Joined: 28 Mar 2007
Posts: 21
Location: India

PostPosted: Tue Apr 24, 2007 7:47 pm
Reply with quote

very clear Phrzby Phil
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Sun Apr 29, 2007 9:28 pm
Reply with quote

Hi Ismail,

You can redefine any variable any way you like (obeying the compiler rules, of course), but you must first have a firm idea of how you plan to use the results. That, you didn't tell us.

Without it we can't determine if your redefine will give you the results you want.

Also your VAR1 field is 4 bytes long and you redefine it with 2 fields totaling 6 bytes. I think this will promt a compiler error.

In any event, you might want to reply and tell us what you want to accomplish AFTER the field is redefined.
Back to top
View user's profile Send private message
ismail.hasan

New User


Joined: 28 Mar 2007
Posts: 21
Location: India

PostPosted: Mon Apr 30, 2007 10:24 am
Reply with quote

Thanks for the reply mmwife.

I in study process. I am not going to implement in any practical envi.?

The thing is the layout which i have mention will work or not...?

and the same time if refined variables length is greater than redfine varibale what will be result.? whether the extra datas will truncate or will through error and what happend if the difference is in negative..?
Back to top
View user's profile Send private message
William Thompson

Global Moderator


Joined: 18 Nov 2006
Posts: 3156
Location: Tucson AZ

PostPosted: Mon Apr 30, 2007 12:32 pm
Reply with quote

ismail.hasan wrote:
and the same time if refined variables length is greater than redfine varibale what will be result.? whether the extra datas will truncate or will through error and what happend if the difference is in negative..?
If the area being redefined (the original area) is larger than the redefining area, this might raise a "W" compile error but nothing is truncated.
If the area being redefined (the original area) is smaller than the redefining area and if the compiler allowed it (it could raise an "S" or "E" compile error) it would allocate enough storage for the longer definition, again nothing would be truncated.
Back to top
View user's profile Send private message
Phrzby Phil

Senior Member


Joined: 31 Oct 2006
Posts: 1042
Location: Richmond, Virginia

PostPosted: Mon Apr 30, 2007 5:44 pm
Reply with quote

Jack -

VAR1 is not 6 bytes - it is 4 bytes - note it is COMP.
Same comment for VAR4 - it is 2 bytes.

S9(1) thru S9(4) COMP are halfwords (2 bytes), as a halfword can hold any 4-digit number. (It can hold some but not all 5-digit numbers.)

Similarly for S9(5) thru S9(9) COMP are all fullwords - 4 bytes.

COBOL note: COBOL gives you what you ask for, so if you have a S9(2) COMP, although a halfword is allocated, COBOL will truncate on the left to maintain at most a 2-digit number, up to +/- 99, as that is what you asked for.
Back to top
View user's profile Send private message
agkshirsagar

Active Member


Joined: 27 Feb 2007
Posts: 691
Location: Earth

PostPosted: Wed May 02, 2007 3:10 pm
Reply with quote

Quote:
COBOL note: COBOL gives you what you ask for, so if you have a S9(2) COMP, although a halfword is allocated, COBOL will truncate on the left to maintain at most a 2-digit number, up to +/- 99, as that is what you asked for.

if I remember correctly, it also depends on TRUNC compiler option too.

see the link below to get an interesting discussion on similar topic.
http://www.ibmmainframes.com/viewtopic.php?t=19569&highlight=trunc
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Sun May 06, 2007 12:15 am
Reply with quote

Hi Phil,

I mis-read the quote below. My old eyes failed to pick up the "redefines" line, thinking that the whole thing was a redefines of the original var1 and added x(4) and 9(2) comp giving 6.
Quote:
2) is it possible to redifne the X variable with comp variable i.e

01 var1 pic x(04)
02 var2 redifnes var1
03 var3 pic s9(02) comp

That's why I said:
Quote:
Also your VAR1 field is 4 bytes long and you redefine it with 2 fields totaling 6 bytes. I think this will promt a compiler error.

Another senior moment. icon_redface.gif
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts Variable Output file name DFSORT/ICETOOL 8
No new posts COBOL - Move S9(11)v9(7) COMP-3 to -(... COBOL Programming 5
No new posts Moving Or setting POINTER to another ... COBOL Programming 2
No new posts parsing variable length/position data... DFSORT/ICETOOL 5
Search our Forums:

Back to Top