Normal strings are common to most programming languages.
They are simply an array of characters, with the end of the string marked by a null character (ASCII zero).
We've already met normal strings (see 2.4 Strings).
The ones we used were constant strings contained in ' characters, and they denote pointers to the memory where the string data is stored.
Therefore, you can assign a string constant to a pointer (to CHAR
), and you've got a ready-filled array with the elements you want (an initialised array.).
DEF s:PTR TO CHAR s:='This is a string constant' /* Now s[] is T and s[2] is i */
Remember that LONG
is actually PTR TO CHAR
so this code is precisely the same as:
DEF s s:='This is a string constant'
The following diagram illustrates the above assignment to s
.
The first two characters s[0]
and s[1]
) are `T' and `h', and the last character (before the terminating null) is `t'.
Memory marked as `Unknown' is not part of the string constant.
+--------+ |Variable| | 's' | |--------| |Address +----* +--------+ \ \ \ +-------+ +--\----+ +-------+ +-------+ +-------+ +-------+ |Unknown| | s[0] | | s[1] | | s[24] | | s[25] | |Unknown| Memory: |+-----+| |+-----+| |+-----+|...|+-----+| |+-----+| |+-----+| || XXX || || "T" || || "h" || || "t" || || 0 || || XXX || +=======+ +=======+ +=======+ +=======+ +=======+ +=======+
E-strings are very similar to normal strings and, in fact, an E-string can be used wherever a normal string can. However, the reverse is not true, so if something requires an E-string you cannot use a normal string instead. The difference between a normal string and an E-string was hinted at in the introduction to this section: E-strings can be safely altered without exceeding their bounds. A normal string is just an array so you need to be careful not to exceed its bounds. However, an E-string knows what its bounds are, and so any of the string manipulation functions can alter them safely.
An E-string (STRING
type) variable is declared as in the following example, with the maximum size of the E-string given just like an array declaration.
DEF s[30]:STRING
As with an array declaration, the variable s
is actually a pointer to the string data.
To initialise an E-string you need to use the function StrCopy
as we shall see.
There are some worked examples in Part Three (see 19 String Handling and I/O) which show how to use normal strings and E-strings.
Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.