Declaration of host variables as pointers in C and C++ embedded SQL applications

You can declare host variables as pointers to specific data types. However, there are some formatting guidelines that you should be aware of.
Before you can declare a host variable pointer, you must consider the following restrictions:
  • If a host variable is declared as a pointer, no other host variable can be declared with that same name within the same source file. The following example is not allowed:
       char mystring[20]; 
       char (*mystring)[20];
  • Use parentheses when declaring a pointer to a null-terminated character array. In all other cases, parentheses are not allowed. For example:
       EXEC SQL BEGIN DECLARE SECTION; 
         char  (*arr)[10];  /* correct   */ 
         char  *(arr);      /* incorrect */ 
         char  *arr[10];    /* incorrect */ 
       EXEC SQL END DECLARE SECTION;

    The first declaration is a pointer to a 10-byte character array. This is a valid host variable. The second is not a valid declaration. The parentheses are not allowed in a pointer to a character. The third declaration is an array of pointers. This is not a supported data type.

    The host variable declaration:
      char *ptr; 

    is accepted, but it does not mean null-terminated character string of undetermined length. Instead, it means a pointer to a fixed-length, single-character host variable. This might not be what is intended. To define a pointer host variable that can indicate different character strings, use the first declaration form shown previously in this topic.

  • When pointer host variables are used in SQL statements, they should be prefixed by the same number of asterisks as they were declared with, as in the following example:
       EXEC SQL BEGIN DECLARE SECTION; 
         char (*mychar)[20];    /* Pointer to character array of 20 bytes */ 
       EXEC SQL END DECLARE SECTION; 
         
       EXEC SQL SELECT column INTO :*mychar FROM table;   /* Correct */
  • Only the asterisk can be used as an operator over a host variable name.
  • The maximum length of a host variable name is not affected by the number of asterisks specified, because asterisks are not considered part of the name.
  • Whenever using a pointer variable in an SQL statement, you should leave the optimization level precompile option (OPTLEVEL) at the default setting of 0 (no optimization). This means that no SQLDA optimization will be done by the database manager.