Referencing pointer host variables in C programs
If you use the DB2® coprocessor, you can reference any declared pointer host variables in your SQL statements.
Procedure
To reference pointer host variables in C and C++ programs:
Specify the pointer host variable exactly as it was declared. The only exception is when you reference pointers to nul-terminated
character arrays. In this case, you do not have to include the parentheses
that were part of the declaration.
Examples of scalar pointer host variable references:
| Declaration | Description | Reference |
|---|---|---|
|
hvshortp is a pointer host variable that points to two bytes of storage. |
|
|
hvdoubp is a pointer host variable that points to eight bytes of storage. |
|
|
hvcharpn is a pointer host variable that points to a nul-terminated character array of up to 20 bytes. |
|
Example of a bounded character pointer host variable reference: Suppose that your program declares the following bounded
character pointer host variable:
struct {
unsigned long len;
char * data;
} hvbcharp;
The following example references this
bounded character pointer host variable: 

hvcharp.len = dynlen; a
hvcharp.data = (char *) malloc (hvcharp.len); b
EXEC SQL set :hvcharp = 'data buffer with length'; c
Note:
- a
- dynlen can be either a compile time constant or a variable with a value that is assigned at run time.
- b
- Storage is dynamically allocated for hvcharp.data.
- c
- The SQL statement references the name of the structure, not an element within the structure.
Examples of array pointer host variable references:
| Declaration | Description | Reference |
|---|---|---|
|
hvarrp1 is an array of 6 pointers that point to two bytes of storage each. |
|
|
hvarrp2 is an array of 3 pointers that point to 8 bytes of storage each. |
|
|
hvbarrp3 is an array of 5 bounded character pointers. |
|
Example of a structure array host variable reference: Suppose that your program declares the following pointer
to the structure tbl_struct:
struct tbl_struct *ptr_tbl_struct =
(struct tbl_struct *) malloc (sizeof (struct tbl_struct) * n);
To
reference this data is SQL statements, use the pointer as shown in
the following example. Assume that tbl_sel_cur is a declared cursor. for (L_col_cnt = 0; L_col_cnt < n; L_con_cnt++)
{ ...
EXEC SQL FETCH tbl_sel_cur INTO :ptr_tbl_struct [L_col_cnt]
...
}