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;

Char Arrays

C-strings are arrays of type char terminated with the null character or \0.

To Declare and Initialise a Character String Array

Character arrays can be declared and initialised on a character-by-character basis using an array-style initialiser however it’s much easier to initialise a character array with a string literal –

char greeting[6] = {'h','e','l','l','o','\0'}; // each array address is initialised individually with NULL character added manually

char greeting[] = "hello" ;//initialisation with a string literal with the compiler calculating the size of the array. The null character is added automatically.

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).

Assign New Values to a Char Array

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 that a program is not allowed to modify. Therefore a pointer used to access a string literal should point to const char. Programmers can allocate their pointers to store and access characters held in string tables. The code sample below creates and prints a string literal –

#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;
}

Attempting to modify a string literal using a pointer can result in undefined behaviour so any pointer to a string literal should be declared constant.

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;
}

wchar_t, char16_t, char32_t

A wchar_t, or wide char is an implementation-defined wide-character type. Its size and the character encoding it represents are platform-dependent. On Windows it is normally 16 bits, while on Linux it is normally 32 bits.

Since the size of wchar_t is compiler-dependent C++ provides char16_t and char32_t as fixed-width character types. char16_t is 16 bits wide and is used with UTF-16 encoded text, while char32_t is 32 bits wide and is used with UTF-32 encoded text.

The wcout object in C++ is an object of the class wostream and is used to send Unicode strings that do not fit in a char variable to the screen. To declare a wide-character string literal it is necessary to put L before the literal.

The following code demonstrates char and widechar arrays with the associated size data.

#include <iostream> 
#include <string.h>
#include<cwchar> 
using namespace std; 
int main() 
{ 
 char str[]="string";
// wide-char type array string 
 wchar_t wstr[]=L"string" ;
 cout << "The size of '" << str <<"' is " << sizeof(str) << endl;
 wcout << "The size of '" << wstr << "' is " << sizeof(wstr) << endl; 
 return 0; 
}