How would this C struct be translated into Cobol -
i'm pretty new cobol , i'm having difficulty figuring out how use structs. c structs below when converted cobol?
these structs have:
struct datat { int m; }; struct stack { int top; struct datat items[stacksize]; } st;
how statement represented in cobol?
st.items[st.top].m
this stab in dark since i've never written line of cobol before today1. however, after little googling2 , playing around in ideone, think i've @ least captured flavor of code like, if not actual solution:
identification division. program-id. ideone. environment division. data division. working-storage section. 01 ws-stack. 05 ws-top pic 9 value 0. 05 ws-items occurs 10 times indexed i. 10 ws-m pic 9 value 0. procedure division. add 1 ws-top. move 9 ws-m(ws-top). add 1 ws-top. move 8 ws-m(ws-top). display "ws-stack :" ws-stack. display "ws-top :" ws-top. display "ws-items[ws-stack.ws-top].m :" ws-m(ws-top). subtract 1 ws-top. display "ws-top :" ws-top. display "ws-items[ws-stack.ws-top].m :" ws-m(ws-top). stop run.
yes, size hardcoded 10 (don't know how symbolic constants in cobol), , ws-top
, ws-m
can store values 0 9.
needless say, data types in cobol , c different. haven't created new stack type; i've declared single data items couple of sub-items, 1 of table can store 10 instances of called ws-m
. same writing
int main( void ) { int top = 10; int m[10]; m[--top] = 9; m[--top] = 8; printf("top = %d\n", top ); printf("m[%d] = %d", top, m[top] ); top++; printf("top = %d\n", top ); printf("m[%d] = %d", top, m[top] ); return 0; }
in c, main difference being wrote c code such stack grows "downwards" (which more natural). far tell in ten minutes spent going through cobol tutorial, cobol not have equivalent struct
type; though data items can grouped in hierarchical manner, you're not creating new struct
or record type such. if wanted multiple stacks, i'd have declare multiple, separate backing stores , index variables.
i think.
i'll have little more reading.
1. @ point in day rather work on anything other problem in front of me right now, , i've been curious how other half lived. also, i'm working on online banking platform , know half our backends written in cobol, wouldn't hurt take sime time learn it.
2. cannot vouch quality of tutorial; it's first 1 found seemed reasonably complete , easy read.
Comments
Post a Comment