DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
No More String Errors - String(3C++)

Declarations

Strings can be declared as static or automatic variables or created, using the new operator, on the free store. Strings are declared in the usual way; their initial value is the null String, unless an explicit initialization is given. The initializer can be a single character, a character pointer, another string, or an expression whose type is one of these. For example:

   #include <String.h>
   int
   foo()
   {
      char*   p = "abc";
      String  a;               // a is the null string
      String  b = "abc";       // b is "abc"
      String  c = p;           // c is "abc"
      String  d("abc");        // -> String d = "abc"
      String  e = 'd';         // e is "d"
      String  f = 1;           // f is "\ 001"
      String  g = d;           // g is "abc"
      String  h = g + e;       // h is "abcd"
      String  i(p,4);          // i is "abc\ 0"
      String  j;  j.reserve(400);  // j is null, but
                               // space is allocated
                               // for >=400 chars
       ...
   }

are all legal String declarations (``+'' is catenate). The declaration String i(p,4); says that String i will contain the 4 contiguous characters that are pointed to by p, "abc" in this case. This is one method for putting a zero byte in a String. The reserve() function can be used on any String to give a hint about the final size of the String. It is included for performance reasons, and should be used in cases where a long String is created by repeatedly appending small pieces. The #include line is required in all programs that use Strings; it will be omitted from the rest of the examples.


Next topic: Operations
Previous topic: No More String Errors - String(3C++)

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004