Your browser doesn't support JavaScript C-Strings – Windows Programming

C-Strings

The C++ character string originated in the C language and continues to be supported within C++. In C programming, the char type is used to store characters. char is an integral type that is used to represent a character. It is stored as a small integer value, and the mapping between character values and characters depends on the character encoding used.

To declare a variable with character type, use the char keyword followed by the variable name –

char ch;

Initialise a char

A char character variable can be initialised with a character literal or an integer type. A character literal contains one character surrounded by a single quotation (‘’).

The following example declares  char variable ch and initialises it with a character literal ‘A’

char ch='A';

Because a char is an integer type, it can also be initialised using an integer. –

char ch=65;

Character Arrays and C-Strings

In C++, a character array is an array of type char that stores a sequence of characters. Character arrays are commonly used to store text, but not every character array is a C-string.

A C-string is a character array that ends with a special character called the null character, written as '\0‘. This character marks the end of the string.

For example:

char letters[5] = {'H', 'e', 'l', 'l', 'o'};

This declares a character array containing five characters. However, it is not a C-string because it has no terminating null character.

To create a C-string, the array must include space for the null character:

char word[] = "Hello";

The compiler automatically adds the terminating '\0', so the array contains six characters in total: the five letters and the null character.

Initialising a char array with the value ‘\0’ creates a NULL or empty string – 

char greeting[0] = {'\0'};

Inserting ‘\0’ anywhere in the middle of the array would not change the size of the array but it would mean that string processing would stop at that point. Sending the following char array to the screen would only produce the characters ‘hel’

char greeting[] = "hel\0lo" 

Single Quotes vs Double Quotes

In C++ single quotes identify a single character and double quotes creates a string literal. ‘a’ is a single character literal, while “a” is a string literal containing an ‘a’ and a null terminator (that is a 2 char array).

Assigning a new value to a String

To change the contents of the string after the initial assignment, it is necessary to change the array contents individually. Trying to assign a new value to an existing char array, i.e. greetings[]=”HELLO”, won’t work because the = operator isn’t defined to copy the contents of a string literal to a char array.

greetings[0] = ‘H’;
greetings[1] = ‘E’;
greetings[2] = ‘L’;
greetings[3] = ‘L’;
greetings[4] = ‘O’;
greetings[5] = ‘\0’;

Since assigning new array string values individually is not very practical, The C-style functions strcpy and strncpy can be used to copy characters into an existing character array. They are declared in <cstring>.

strcpy(greetings,"hello");

Important: the destination array must have enough space. strcpy does not check the size of the destination array.

Pointers and Arrays

The use of pointers can also access array elements. The pointer is declared and assigned to the first element of the array. After assigning the array pointer, the individual elements can be accessed by increasing or decreasing the pointer value.

The code section below outputs the same letters of an array string by using pointers and array indexing

#include <iostream>
using namespace std;
int main()
{
char str[30]="this is a string to array test"; //declares char arrar
char *pChar=str;//declares char pointer and sets to start of array
int i;
for (i = 0; i < sizeof(str); i++){
cout << static_cast<void*>(pChar + i); //outputs pointer value
cout << str[i] ; //outputs array value
}
return 0;
}

Pointers and String Literals

A string literal in C++ is a sequence of characters enclosed in double quotation marks. For example, “Hello World” is a string literal.

String literals must not be modified. Therefore, when a pointer is used to access a string literal, it should point to const char, indicating that the characters cannot be changed through that pointer.

The following example creates a pointer to a string literal and prints its contents:

#include <iostream>
using namespace std;
int main()
{
const char *ptrsl= "this is a string literal."; //creates pointer ptrsl to to start of string array
cout << ptrsl;
return 0;
}

Array of Pointers

An array of pointers to strings is a sequence of char pointers where each pointer points to the first character of a sequence of strings. To declare and initialise an array of pointers to strings.

char *day[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday","Saturday"};

Since initialisation of the array is done at the time of declaration then the size of the array can be omitted.

To access the values pointed to by an array of pointers it is necessary to iterate and de-reference each pointer individually.

In the code section below, each element of the array day is a pointer to the base address of the first character of an individual string literal which is then output using a for-next loop.

#include <iostream>
#include <cstdio>
const int MAX = 7; 
int main () 
{ 
const char *day[] = {"Sunday","Monday","Tuesday", "Wednesday", "Thursday","Friday","Saturday"}; 
int i = 0; 
for ( i = 0; i < MAX; i++){
printf("day[%d] = %s\n", i, day[i] ); 
}
return 0;
}

Wide Character Types

In C++, wchar_t, char16_t, and char32_t are character types designed to represent character data using different character encodings and code-unit sizes. They can be useful when working with text that requires Unicode encoding.

A wchar_t, or wide character, is an implementation-defined character type. Its size and the encoding used to represent characters depend on the platform. On Windows, it is normally 16 bits wide, while on Linux it is normally 32 bits wide.

Because the size of wchar_t is platform-dependent, C++ also provides char16_t and char32_t as fixed-width character types. char16_t is 16 bits wide and is commonly used for UTF-16 encoded text, while char32_t is 32 bits wide and is commonly used for UTF-32 encoded text.

The wcout object is an instance of the std::wostream class and is used to output wide-character data to a stream, typically the console. However, it does not automatically guarantee correct Unicode output; the result depends on the stream configuration and platform.

To declare a wide-character literal, place the prefix L before the literal. For example:

#include <iostream>
using namespace std;

int main() {
    wchar_t letter = L'A';
    wcout << letter << endl;
    return 0;
}

In this example, L‘A’ is a wide-character literal, and wcout outputs the character.

The prefixes u and U are used for UTF-16 and UTF-32 literals, respectively:

char16_t letter16 = u'A'; char32_t letter32 = U'A';

Note: char16_t and char32_t represent UTF code units, not necessarily individual user-perceived characters. For example, some Unicode characters require two UTF-16 code units.