2. Embedded SQL for C : C Variables and Data Types : Variable Usage : Array Variables
 
Share this page                  
Array Variables
The following syntax refers to an array variable:
:arrayname [subscript] {[subscript]}
Note:   
You must subscript the variable, because only scalar-valued elements (integers, floating-point and character strings) are legal SQL values.
When you reference the array, the number of indices is noted but the embedded SQL preprocessor does not parse the subscript values. Consequently, even though the preprocessor confirms that you used the correct number of array indirections, the preprocessor accepts illegal subscript values. You must make sure that the subscript is legal. For example, the preprocessor accepts both of the following references, even though only the first is correct:
float salary_array[5];
:salary_array[0]
:salary_array[+-1-+]A character string, declared as an array of characters, is not considered an array and cannot be subscripted in order to reference a single character. In fact, single characters are illegal string values, as all character string values must be null-terminated.
For example, if the following variable were declared:
static char abc[3] = {'a', 'b', 'c'};
you could not access the character "a" with the reference:
:abc[0]
To perform such a task, declare the variable as an array of three single character strings:
static char *abc[3] = {"a","b","c"};
As with standard C, any variable that can be denoted with array subscripting can also be denoted with pointers. This is because the preprocessor only records the number of indirection levels used when referencing a variable. The indirection level is the sum of the number of pointer operators preceding the variable reference name and the number of array subscripts following the name. For example, if a variable is declared as an array:
int age_set[2];
it can be referenced as either an array:
:age_set[0]
or a pointer:
:*age_set
Do not precede references to elements of an array with the ampersand operator (&) to denote the address of the element.
Any arrays of indicator variables that you use with structure assignments must not include subscripts.
The following example uses the variable "i" as a subscript. This variable does not need to be declared in the declaration section, as it is not parsed.
Example: Variable as subscript usage
exec sql begin declare section;
  char *formnames[3={"empform","deptform","helpform"};
exec sql end declare section;
 int I;
 for (i = 0; i < 3; i++)
         exec frs forminit :formnames[i];