VARCHAR pointers in Pro*C

From: Tim Smith <tssmith_at_netcom.COM>
Date: 26 Jan 92 18:23:00 GMT
Message-ID: <1992Jan26.182300.969tssmith_at_netcom.COM>


Here is some more on the issue of pointers to VARCHARs in Pro*C.

You can declare and use a pointer to a VARCHAR, but you must allocate the VARCHAR structure, using malloc or another C library memory allocation function. Furthermore, you must set the length member to an appropriate value before SELECTing into the struct pointed to by the VARCHAR pointer.

So, you can say

EXEC SQL BEGIN DECLARE SECTION;
    VARCHAR *foo;
EXEC SQL END DECLARE SECTION; but you CANNOT say

EXEC SQL BEGIN DECLARE SECTION;
    VARCHAR *foo[10];
EXEC SQL END DECLARE SECTION; as this tries to declare an array of pointers to VARCHARs. Arrays of pointers are not supported. If you do the latter, you'll get a PCC-S-0069 "unsupported datatype" error.

The section below is slightly adapted from Chapter 1 of the V1.4 Pro*C Supplement to the ORACLE Precompilers Guide:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Pointer to a VARCHAR


On Input:



When you use a pointer to a VARCHAR as an input host variable, you must allocate enough memory for the expanded VARCHAR declaration. Then, you must place the desired string in the array member and set the length member, as shown in the following example:

    EXEC SQL BEGIN DECLARE SECTION;

        VARCHAR *emp_name;
        VARCHAR  emp_name2[10];

    EXEC SQL END DECLARE SECTION;     ...
    emp_name = malloc(sizeof(short) + 10) /* len + arr */     strcpy(emp_name->arr, "MILLER");
    emp_name->len = strlen(emp_name->arr);

Or, to make emp_name point to an existing VARCHAR (emp_name2 in this case), you could code the assignment

    emp_name = &emp_name2;

then use the VARCHAR pointer in the usual way, as in

    EXEC SQL INSERT INTO EMP (EMPNO, ENAME, DEPTNO)         VALUES (:emp_number, :emp_name, :dept_number);

If you look at how the Pro*C Precompiler expands the declaration

    EXEC SQL BEGIN DECLARE SECTION;
        VARCHAR *emp_name;
    EXEC SQL END DECLARE SECTION; you see that emp_name is declared as a pointer to a struct with an array member of length 1. However, since emp_name has been reassigned to emp_name2, it effectively has the array size of emp_name2.

On Output:



When you use a pointer to a VARCHAR as an output host variable, the program interface determines its maximum length by checking the length member (emp_name->len in our example). So, your program must set this member before every fetch. The fetch then sets the length member to the actual number of characters returned, as the following example shows:

    emp_name->len = 10; /* Set maximum length of buffer. */     EXEC SQL SELECT ENAME INTO :emp_name WHERE EMPNO = 7934;     printf("%d characters returned to emp_name",

            emp_name->len);

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Tim Smith (tssmith_at_netcom.com) Received on Sun Jan 26 1992 - 19:23:00 CET

Original text of this message