XML

kent logo

CO631 Anonymous Questions and Answers Keyword Index

This page provides a keyword index to questions and answers. Clicking on a keyword will take you to a page containing all questions and answers for that keyword, grouped by year.

To submit a question, use the anonymous questions page. You may find the keyword index and/or top-level index useful for locating past questions and answers.

Keyword reference for strings

2003

Question 104 (2003):

I want to have an array of strings (which I have understood to be an array of bytes) but am unsure how to use this. Is it:

    [element_length][index_location]string_array:

And also how would I write into a specific location, e.g. put "hello world" into index `0' in the string array?

Answer 104:

You are along the right lines with your declaration. More correctly:

    [number.of.strings][string.length]BYTE string.array:

The important thing to remember (as was covered to some extent in `q6') is that occam does not have an explicit `string' type -- they're handled using arrays of BYTEs. Thus you need to cater for `string length' separately (or use `null'-padding on the strings).

The above declaration declares an array of arrays of BYTEs. In other words, an array of fixed-size strings. If all the strings are going to be constant, declare it as a VAL array, e.g.:

    VAL [][]BYTE string.array IS ["hello world   ",
                                  "from occam    ",
                                  "but not from C"]:

Note that all the `strings' are the same-length -- the compiler will reject the declaration if this is not the case.

If you go down the `variable' route, data can be placed in the array using standard assignment, but the types must match exactly -- `[4]BYTE' and `[6]BYTE' are different types, for example. Thus assignments will often end up using array slices. See Question 44 (2002) for a comprehensive description of these. E.g.:

    [10][20]BYTE string.array:
    SEQ
      [string.array[0] FOR 11] := "hello world"          -- type is [11]BYTE
      [string.array[0] FROM 11] := "         "           -- pad remaining BYTEs with spaces

or this could be done as a single assignment:

      string.array[0] := "hello world         "          -- type is [20]BYTE

Keywords: arrays , strings

Valid CSS!

Valid XHTML 1.0!

Last modified Mon May 15 17:39:48 2006
This document is maintained by Fred Barnes, to whom any comments and corrections should be addressed.