Char array versus string

About the difference between c-strings supported by C/C++ and string template class in C++

Today’s tidbit is a very “basic” one. It’s about the difference between char array and string in C plus plus. If you have started with a low level language, this is a real deal ! Read on, to understand what this means. In short – the question we will try and answer today is : “Is there any difference between the two options below :”

string input= "Geekgirl"; //input is a string variable
char input[]="Geekgirl"; // input is a char array

Char Arrays

As suggested by the name char arrays are an array of “characters”. An example is as shown below. These are by default supported in C and C++. Strings are represented or implemented as char arrays that are null terminated in C language. Char arrays are hence also called the “c-strings“.


Fig 1. Char array with null terminated character ย ‘\0’ (c-string)

// Usage of char arrays
//A. Declare char array
char input_text[10]; 
//Assign value via command line input

//B. Declare char array and assign value
char input_text[]={'G','e','e','k','\0'}; 

//C. Declare and assign value using double quote enclosed literal constants
//Literal constants are by automatically null terminated (String literals)
char input_text[]="Geek";

//D. using char*
char* input_text_ptr = "Geek";

Since above are char array declarations, all rules of a regular array apply to them. They cannot be assigned a value once they have been declared.

//Once input_text is declared and assigned a value as in B and C options 
above, the following assignment operations are invalid:
input_text ="Geek"; // not allowed
input_text[]="Geek"; //not allowed
input_text ={'G','i','r','l','\0'}; // not allowed

//But, they can be edited using indexing into individual elements as below:
//valid assignments listed below.

// But, when declared using char* (pointer to character) as in case D above: :
*(input_text_ptr+0) ='D'; // not allowed. 
// But, if we allocate memory 
// and then assign a value, then we can assign char value as above:
char *input_text_ptr= (char *)malloc(sizeof(char)*size); /*Stored in heap segment*/
ย *(input_text_ptr+0) = 'D'; //valid

As in case of normal arrays, the name input_text is nothing but a pointer to the start of char array: address of input_text[0].

char* temp = input_text; // using char pointer 
char temp[] = input_text; // invalid operation

//below is valid 
char temp[5]; 


So, what is a string in C++ then? Well, string class in C++ provides support for common string related operations such as compare, search, swap, copy and also offer dynamic sizing ย , exception handling when there is access out of bounds.

Char arrays have a “fixed” size specified either implicitly or explicitly during declaration. But, strings do not require it. This is because strings are dynamically sized during runtime, where as char arrays are sized during compile time.

string mystr = "geekgirl"; // has no size or [] in the declaration
// we are creating a string object named mystr above
char mystr[] = "geekgirl"; // c style string declaration

Memory allocation

The memory for char arrays is allocated on stack unless malloc() is used to create a char array. In case malloc() is used, memory is allocated on the Heap and the user is responsible for releasing the memory after use. Since, cstrings are created with definite size in mind, it is harder to resize them, user has to be careful about invalid access. Think of String object as a utility in C++ that manages the dynamic allocations for us and provides an easier interface to perform operations on char arrays.

Passing to a function

String passing to a function is similar to how char arrays are passed, as in the example below.

char str[]="geek";

string str1="geek";

// passing char array to a function
void displayCharArray(char s[])
 cout << "Char array contains" << s <<;

// passing string to a function
void displayString(string s)
 cout << "String contains ย " << s << endl;

Which one should one use?

In case you are implementing code to be compatible with legacy C code, then you may be better off using char arrays. String class objects are easy to use, provide features like bound check exceptions and manage the allocations – sizing and dynamic resizing, managing the free operations on the memory allocated for the user string. So, if you are using C++ and no legacy code to deal with, you could go for string objects.

As always, think of your use case and decide which one is suitable for your case. That’s all in this tidbit. There is a tiny mention about using char* in this blog (pointer to char), but will cover it in another tidbit. Until then, keep decoding!~


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: