Streams - DePaul University



cstring

1. A cstring is an array of characters that C++ gets from its earlier C language implementation. A cstring is a series of characters terminated by a null character ('\0'). Functions that receive cstrings as parameters will, process the cstring from its first element [0] until the element containing the null character ('\0'). The '\0' is used as a terminating sentinel.

Not all char arrays are cstrings. One element of the char array cstring must be used to hold a terminating null character.

int main()

{

char a[10] = {'y','e','s'}; // NOT a string

char s[10] = "yes"; // cstring, s[3] has '\0' from system.

char t[4] = "yes"; // cstring, with just enough space

char y[10] = {'y', 'e', 's', '\0', 'n', 'o','\0'};

char z[10] = {'1', '2', '3', '4', '\0'}; // char or number?

}

2. Declaration and initialization.

int main()

{

char s1[8] = "CSC 215"; // spaces are allowed in cstring

char s2[] = "hello"; // if the length is not specified,

// compiler automatically determines

// the size needed

char s3[20]; // uninitialized

3. Operations with cstrings.

int main()

{

char s1[5] = "john";

char s2[5] = "jack";

char s3[20];

// Cannot do the following with cstrings

if (s1 == s2) // NO comparison by ==, same as any array

...

s1 = s2; // NO assignment by =, same as any array

// Can do the following with cstrings

cout > s3; // OK. cstring input until white space.

// Null terminator is automatically added

// at the end.

cout ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download