(Contents)(Previous)(Next)

VARCHAR

The data type 'VARCHAR' may be used for the declaration of variables to which character strings of variable length are assigned. A VARCHAR declaration must contain at least either an array declarator or a pointer declarator. The last array declarator defines the maximum length ( <= 32767 ) of the variable. For a VARCHAR declaration with pointer declarator, the maximum length is undefined; the program has to assign storage space during runtime. The current length of a VARCHAR variable is defined by the length field, zero bytes are ignored in the calculation of the length. The precompiler replaces the declaration of VARCHAR variables by a structure declaration:

VARCHAR v {n}; is replaced by

struct {

unsigned short len;

unsigned char arr [n];

} v; where

len is assigned the current length

of the character string and

arr is assigned the characters.

VARCHAR *v; is replaced by

struct {

unsigned short len;

unsigned char arr [1];

} *v;

Examples of valid varchar declarations are:

VARCHAR a [21], b [100] [133]; /* a is a variable of length

21 and b an array of 100

elements of length 133 */

typedef VARCHAR LONGSTRING [ 65534 ]; /* type definition */

LONGSTRING c, d; /* c and d are variables of

type LONGSTRING */

typedef VARCHAR *PVC; /* definition of the pointer

type PVC */

PVC p; /* declaration of the VARCHAR

pointer p */

For example, the following statements can be used to assign storage space to p:

n = 100; /* maximum length of the VARCHAR variable */

p = (PVC) malloc (sizeof (p->len) + n * sizeof (p->arr));

VARCHAR pointers of a fixed maximum length can be declared in the following way:

typedef VARCHAR VC30 [30]; /* VARCHAR type of length 30 */

VC30 *q; /* q is a pointer to a VARCHAR of the

maximum length 30 */

The following statement is used to assign storage space to q:

q = (VC30* ) malloc (sizeof (VC30));


(Contents)(Previous)(Next)