| Home | Why Omnibasic | Features | FAQ | Examples |
|---|---|---|---|---|
| Reviews | Keyword/Syntax | On-Line Manual | Download Manual | ScreenShots |
Chapter 8Pointer Variables and Vectors Pointers are a mainstay of the programming language “C”, but the concept of pointer variables was foreign to BASIC (unitl now). The challenge of adding pointers to OmniBasic was to provide the power of the concept but to do so with a syntax that has a BASIC flavor rather than the cryptic style of “C”. Pointers are variables in their own right and are a special form of a 32 bit unsigned integer which have a special property to index their value according to the data type which they are declared to point to. For example, pointers can be assigned values and used in expressions just like the data type long (integer). This allows the pointer to be set to any value from 0 to $ffffffff and this fact alone gives the pointer concept extreme power as well as the power to cause a catastrophe if used carelessly. A more useful setting of a pointer would be to set it to the address of another variable or to the address of a memory buffer (see BUFFER, BUFADR(), BUFSIZ() section). When setting a pointer to another variable it is the data type of the pointer, which prevails when accessing data with the pointer (not the data type of the variable being pointed to). Most “C” compilers give a warning if a pointer is set to point to a variable with a different data type than its own; however, OmniBasic gives no such warning. The functions ADDR() and INDEX() are intended primarily for use with pointers although the ADDR() function could be used to find the address of a variable for any other reason. Also, the BUFADR() function is intended to set pointers to the base address of memory buffers. The ADDR() function returns the memory address of a variable given as the argument to the function. It is used primarily to set a pointer to the absolute address of a variable. In the case of an array, the address returned is the base address (address of the first element of the array). The ADDR() function could also be used in address calculations to determine the storage relationship of two or more variables. To reference data pointed to by a pointer the pointer variable is enclosed in square brackets i.e. [Ptr]. Data referenced by pointers must be of a compatible type with the variable or constant used with it just as if the data were to/from a standard variable (I.E. not pointed to by a pointer). The INDEX function is used to point to different elements of the object data (assuming the object is an array or is being treated as an array). In “C” arithmetic operations on pointers take into account the data type of the pointer. In OmniBasic arithmetic operations on pointers do not take the data type into account. The INDEX() function does take the pointer’s data type into account however. The arguments to the INDEX() function are the pointer name and the index value. To index backward a negative index value is specified. In the following example the INDEX() function will be used on a pointer whose data type is INTEGER (32 bits or 4 bytes long). When we index the pointer by three it will point 12 bytes past its previous setting (4 bytes times 3=12). The INC and DEC statements also take into account the data size pointed to by a pointer. If Ptr is a an INTEGER pointer then, INC Ptr will add 4 to the value of Ptr since an INTEGER is 4 bytes in size
Vectors are another form of variable that can be used to achieve similar results as pointers. The vector data type is a compiler defined internal data structure, which among other elements contains a pointer. The effect of using a vector instead of a pointer is that the same results may be achieved, but without any special syntax. The only requirement is that the vector’s internal pointer must be set to the address of the data to be referenced before the vector is used. Once this setting has taken place, the vector may be used as if it were an ordinary variable. The following example shows how to use a vector to reference a STRING variable as if it were a BYTE array.
This will print ‘Bat’. More useful applications include arranging bytes inside a LONG data type by declaring a BYTE array vector, which is assigned the address of a LONG variable. This allows high-speed conversions for byte level multi-platform applications. |
Home || Why Omnibasic || Features || FAQ || Examples
Reviews || Links || Privacy Statement || Top of Page
Innomation Systems, Inc.
117 Morrison Ave. Morrison, MO 65061 (573) 294-6130
OmniBasic is a
trademark of Innomation Systems, Inc., other trademarks are the property of their
respective owners.
Innomation Systems, Inc. reserves the right to change prices and specifications without prior notice.
Copyright © 2000, 2001, 2002 Innomation Systems, Inc.