I have an external COBOL-IT function I want to call from my C program. Getting the error "ERRORC2065: 'productid': undeclared identifier

StackOverflow https://stackoverflow.com/questions/20820075

  •  22-09-2022
  •  | 
  •  

Question

So I have an external COBOL-IT function I want to call from my C program. At the moment I keep getting the error "ERRORC2065: 'productid': undeclared identifier. Same goes for "aantal". My C is a little rusty since I haven't used it in some time so if anyone could help me out, that would be great. If I drop the "&" it also doesn't work.

#include <stdio.h>
#include "libcob.h"
//#pragma linkage (verkoop, COBOL)

extern void verkoop(char *productid, char aantal*);

int main(int argc, char *argv[])
{
    COB_RTD = cob_get_rtd();
    cob_init(rtd, 0, NULL);
    char productid[6] = "000020";
    char aantal[6] = "000200";
    printf("Hello world");  
    verkoop(&productid, &aantal);
    return 0;
}

This is my verkoop function for those who are interested:

*************************************************************
* VERKOOP  
*************************************************************
       IDENTIFICATION DIVISION.
       PROGRAM-ID. VERKOOP.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT PRODUCTEN ASSIGN TO "BESTANDEN/PRODUCTEN"
           ACCESS MODE IS RANDOM
           ORGANIZATION IS INDEXED
           RECORD KEY IS PRODUCTID
           FILE STATUS IS WS-FILE-STATUS.

       DATA DIVISION.
       FILE SECTION.
       FD  PRODUCTEN BLOCK CONTAINS 10 RECORDS.
       01  PRODUCT.
           02 PRODUCTID PIC X(6).
           02 LEVERANCIERID PIC X(6).
           02 AANTAL PIC 9(6).
       WORKING-STORAGE SECTION.
       77  FOUT PIC X.
           88 PRODUCT-NIET-GEVONDEN VALUE 1.
       77 WS-PRODUCTID PIC X(6).
       77 WS-AANTAL PIC 9(6).
       77 WS-FILE-STATUS PIC XX.
       LINKAGE SECTION.
       01 LS-PRODUCTID PIC X(6).
       01 LS-AANTAL PIC 9(6).
       PROCEDURE DIVISION USING LS-PRODUCTID, LS-AANTAL.

       MAIN.
           PERFORM INITIALISEER
           PERFORM LEES-PRODUCT-IN
           PERFORM LEES-BESTAND
           PERFORM SLUIT-BESTAND
           STOP RUN.

       INITIALISEER.
           MOVE ZEROS TO PRODUCT
           OPEN I-O PRODUCTEN.
*          DISPLAY WS-FILE-STATUS..

       LEES-PRODUCT-IN.
           MOVE LS-PRODUCTID TO WS-PRODUCTID
           MOVE LS-AANTAL TO WS-AANTAL.
*          DISPLAY "GEEF PRODUCTID OP: "
*          ACCEPT WS-PRODUCTID
*          DISPLAY "GEEF AANTAL OP: "
*          ACCEPT WS-AANTAL.

       LEES-BESTAND.
*      DISPLAY "LEES-BESTAND"
       MOVE WS-PRODUCTID TO PRODUCTID
*      DISPLAY PRODUCTID
       READ PRODUCTEN INVALID KEY SET PRODUCT-NIET-GEVONDEN TO TRUE
       END-READ   
       DISPLAY "END-READ" WS-FILE-STATUS    
       IF PRODUCT-NIET-GEVONDEN PERFORM FOUTJE    
       ELSE 
       MOVE WS-PRODUCTID TO PRODUCTID
       SUBTRACT WS-AANTAL FROM AANTAL   
       PERFORM UPDATE-PRODUCT
       END-IF.


      UPDATE-PRODUCT.
        REWRITE PRODUCT INVALID KEY PERFORM FOUTJE.

        SLUIT-BESTAND.
*       DISPLAY "SLUIT-BESTAND"
           CLOSE PRODUCTEN.

        FOUTJE.
           DISPLAY "ER IS EEN FOUT OPGETREDEN"
*          DISPLAY WS-FILE-STATUS
           STOP RUN.

UPDATE: I fixed the problem by declaring productid and aantal on the first lines of my code.

Was it helpful?

Solution

1) Your constant character array is too small to hold the value of the null terminated initial string.

2) As well, your declaration of the function is incorrect.

extern void verkoop(char *productid, char aantal*);

should be

extern void verkoop(char *productid, char *aantal);

(HINT: Look at '*' for aantal);

3) You call the function with invalid parameters. The function call should have been

verkoop(productid, aantal);

Could you share more info about the compiler that you're using ?

OTHER TIPS

Here's the sample C program from your Getting Started manual:

/* hello.c */
#include <libcob.h>
extern int say(char *hello, char *world);
int main()
{
COB_RTD = cob_get_rtd();
int ret;
int return_status;
char hello[7] = "Hello ";
char world[7] = "World!";
cob_init(rtd, 0, NULL);
ret = say(hello, world);
cob_stop_run (rtd, return_status);
return ret;
}

This shows a simple way to exercise the COBOL-IT API from C.

Note that COBOL does not have "strings". A six byte field in COBOL occupies six bytes, and is not terminated by a "null", or anything else.

Data passed "by reference" to a COBOL program can be amended in the COBOL program. It is up to the programmer to ensure that it is not if it shouldn't be.

You must have other compile errors. Your declarations for productid and aantal are not correct.

char productid[6] = "000020";
char aantal[6]    = "000200";

Compiling that with VC++ shows, error C2117: 'productid' : array bounds overflow

They are short one byte, for the null termination in the string literal. Why not just do:

char *productid = "000020";
char *aantal = "000200";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top