The Varying Length String Type
All C character strings are
null-terminated. (For more information, see
The Character String Data Type (see
The Character String Data Type). Ingres data of type
char or
varchar can contain random binary data including the zero-valued byte (the null byte or "\0" in C terms). If a program uses a C
char variable to retrieve or set binary data that includes nulls, the EQUEL runtime system is not able to differentiate between embedded nulls and the null terminator. Unlike other programming languages, C does
not blank-pad fixed length character strings.
In order to set and retrieve binary data that may include nulls, a new EQUEL/C storage class, varchar, has been provided for varying length string variables. varchar identifies the following variable declaration as a structure that describes a varying length string, namely, a 2-byte integer representing the count and a fixed length character array. Like other storage classes previously described, the keyword varchar must appear before the variable declaration:
## varchar struct {
## short current_length;
## char data_buffer[MAX_LENGTH];
## } varchar_structure;
Syntax Notes:
• The word varchar is reserved and can be in uppercase or lowercase.
• The varchar keyword is not generated to the output C file.
• The varchar storage class can only refer to a variable declaration, not to a type definition. For example, the following declaration is legal because it declares the variable "vch":
## varchar struct {
## short buf_size;
## char buf[100];
## } vch;
But the varchar declaration of the structure tag "vch" (without a variable) is not legal and will generate an error:
## varchar struct vch {
## short buf_size;
## char buf[100];
## };
• The structure definition of a varchar variable declaration can be replaced by a structure tag or typedef reference. For example the following typedef and varchar declarations are legal:
## typedef struct vch_ {
## short vch_count;
## char vch_data[VCH_MAX];
## } VCH;
## varchar VCH vch_1; /* typedef referenced */
## varchar struct vch_ vch_2;
## /* structure tag referenced */
• The varchar storage class can be used for any type of variable declaration, including external and static variables, and to qualify nested structure members. For example, the following declarations are all legal:
## static varchar struct _txt {
## /* with storage class "static"*/
## short tx_len;
## char tx_data[TX_MAX];
## } txt_var, *txt_ptr, txt_arr[10];
## struct {
## char ename[20];
## int eage;
## varchar struct_txt ecomments;
## /* nested in structure */
## } emp;
## typedef short BUF_SIZE;
## typedef char BUF[512];
## varchar struct {/* members are typedef'd */
## BUF_SIZE len;
## BUF data;
## } vchar;