SQL/MX 3.2.1 Programming Manual for C and COBOL (H06.26+, J06.15+)

Host Variables in C/C++ Programs
HP NonStop SQL/MX Release 3.2.1 Programming Manual for C and COBOL663854-005
3-20
Variable-Length Character Data
Declaring a VARCHAR Host Variable
In addition to the C char data type, SQL-99 provides the VARCHAR data type for host
variables within embedded SQL C/C++ programs. For host variables declared as the
VARCHAR data type, the SQL C/C++ preprocessor generates a C char data type.
When you declare a VARCHAR array as a host variable, the SQL C/C++ preprocessor
by default reserves the last character of the array as a placeholder for a null terminator.
Therefore, declare a VARCHAR array one character longer than the actual number of
required characters.
Example
This example uses a declaration for an SQL column up to 20 characters in length:
EXEC SQL BEGIN DECLARE SECTION;
VARCHAR last_name[21]; /* 20-character last name */
EXEC SQL END DECLARE SECTION;
...
Follow the guidelines outlined in Fixed-Length Character Data on page 3-17 for
handling the null terminator when you declare and use VARCHAR arrays as host
variables for variable-length string literals.
Using the VARCHAR compatible structure to hold VARCHAR
data
In addition to the VARCHAR data type, you can use the following structure to store
VARCHAR data. You can use the structure as the input and the output host variable
instead of the varchar variable:
Struct{
short len;
char val[size];
}<struct name>;
where:
len is a numeric data item that represents the length.
val is a fixed-length character data item for the string.
C