char Array Pointers

A string can be assigned to an array as follows

 

char arrString[] = "Saturn" ;

 

The compiler converts the string of literal characters in to an array, terminated with a NULL.

 

It is therefore possible to assign the string directly to a char pointer (which is in effect the same thing as above):

 

char *ptrString = "Titanium" ;

 

Both can be used in the same manner:

#include <iostream>
using namespace std;

int main () {
	//C style string == array of characters automatically terminated with NULL
	//this is a string literal, consisting of a string of constant values

	char arrString[] = "Saturn" ;
	char *ptrString = "Titanium" ;

	cout << *(arrString+4) << endl ;
	cout << *(ptrString+4) << endl ;

	cout << arrString[4] << endl ;
	cout << ptrString[4] << endl ;

	cout << arrString << endl ;
	cout << ptrString << endl ;

	return 0;
}

Compile & Run:

r
n
r
n
Saturn
Titanium

Leave a Reply