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
- Examples: Scalar pointer host variable references
-
Table 1. Example references to scalar pointer host variables Declaration Description Reference short *hvshortp;
hvshortp is a pointer host variable that points to two bytes of storage. EXEC SQL set:*hvshortp=123;
double *hvdoubp;
hvdoubp is a pointer host variable that points to eight bytes of storage. EXEC SQL set:*hvdoubp=456;
char (*hvcharpn) [20];
hvcharpn is a pointer host variable that points to a nul-terminated character array of up to 20 bytes. EXEC SQL set: *hvcharpn='nul_terminated';
- Example: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: Array pointer host variable references
-
Table 2. Example references to array pointer host variables Declaration Description Reference short * hvarrpl[6]
hvarrp1 is an array of 6 pointers that point to two bytes of storage each. EXEC SQL set:*hvarrpl[n]=123;
double * hvarrp2[3]
hvarrp2 is an array of 3 pointers that point to 8 bytes of storage each. EXEC SQL set:*hvarrp2[n]=456;
struct { unsigned long len; char * data; } hvbarrp3[5];
hvbarrp3 is an array of 5 bounded character pointers. EXEC SQL set :hvarrp3[n] = 'data buffer with length'
- Example: Structure array host variable reference
- Suppose that your program declares the following pointer to the structure tbl_struct:
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.struct tbl_struct *ptr_tbl_struct = (struct tbl_struct *) malloc (sizeof (struct tbl_struct) * n);
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] ... }