• Networking
  • Programming
  • DBMS
  • Operating System
  • Internet
  • Hardware
  • Software

Tech Differences

Know the Technical Differences

Difference Between Character Array and String

Character-Array-string-1C++ supports both, Character array and string, as C++ has considerable benefits in using both of them. But, inability to operate on character array raises the development of class string. Both a character array and string contain the sequence of characters.

But the fundamental difference between character array and string is that the “character array” can not be operated with standard operators, whereas, the “string “objects can be operated with standard operators. Let’s study the other differences between a character array and string.

Content: Character Array Vs String

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison Chart

Basis for ComparisonCharacter ArrayString
BasicCharacter array is collection of variables, of character data type.String is class and variables of string are the object of class "string".
Syntaxchar array_name [size];string string_name;
IndexingAn individual character in a character array can be accessed by its index in array.In string the particular character can be accessed by the function "string_name.charAt(index)".
Data TypeA character array does not define a datatype.A string defines a datatype in C++.
OperatorsOperators in C++ can not be applied on character array.You may apply standard C++ operator on the string.
BoundaryArray boundaries are easily overrun.Boundaries will not overrun.
AccessFast accessing.Slow accessing.

Definition of Character Array

A character array is a collection of the variables of “char” datatype; it can be a one-dimensional array or a two-dimensional array. It is also called “null terminated string”. A Character array is a sequence of the characters that are stored in consecutive memory addresses. In a character array, a particular character can be accessed by its index. A “Null character” terminates the character array”.

Character-Array-1

Let’s take an example of character array:-

char name[ ]={'A', 'j', 'a', 'y', '\0'};
or
char name[ ]="Ajay";

Here, “char” is a character data type, “name” is a variable name of the character array. I had shown two ways to initialize the character array. In the first method, the null is explicitly mentioned and in the second method, the compiler automatically inserts the null.

The end of the string is always a null character; it is the terminating character of the character array. A character array is not a built-in data type; we create character array by declaring it. You can not apply standard operators on a character array.

To operate on character array there are some built-in function such as,(strlen( ), strlwr( ), strupr( ), strcat( )). As the standard operators can not be applied to a character array, they cannot take part in any expression.

The character pointer to a character array can also be created.

Let’s understand it with an example.

char s1[ ]="Hello";
char s2[ ]="Sir";
s1= s1+s2; //error operators can not be applied
s2=s1; //error

Character pointer

char *s= "Morning";
char *p;
p=s; //executes

In above example, we had declared two character array s1, s2 and two character pointers s and p. Character array s1 and s2 are initialized, we can see that neither the addition operator(+) nor the assignment operator works on the character array. But a character pointer can be assigned to another character pointer.

Remember once the character array is initialized it can not be initialized again to another set of character. The access to a character array or a null terminated string is fast as compared to string in C++.

Definition of String

A string is not a built-in data type of C++. It a class object of type “string”. As in C++ creating a class is just like creating a “type”. The class “string” is a part of C++ library. It holds the set of character or character array as a whole. There are three reasons behind the development of a standard string class.

  • First is “consistency”, the character arrays are not data types in their own right.
  • Second is “convenience”, you can not use standard operators on a character array.
  • Third is “safety”, array boundaries are easily overrun.

String-Explanation-1

Let us understand strings with an example.

string s1;
s1= "Hello";
string s2("Good morning");
string s3="Hennery";
string s4;

In the above declaration, four string variable or objects (s1, s2, s3, s4) are declared.  In above declaration, I had shown three ways of initializing the string. The string s1 is declared and then separately initialized. The string s2 is initialized by the constructor of class “String”. The string s3 is initialized at the time of its declaration as normal data type do. We can apply the standard operator to the string variables.

s4=s1;// assigning one string object to other
s4=s1+s2; // adding two string and storing the result in third string
if(s3 > s2) // comparing two strings
strings s5(s1); creating a new string object using existing string object

In above code, various operators are applied on a string and various operations are performed. The first statement copies one string object to another string object. In the second statement, two string are concatenated and stored in a third string. In the third statement, two string are compared. In the fourth statement, a new string object is created using the already existing string object.

Access to the string is slow as compared to a character array or null terminated string.

Key Differences Between Character Array and String

  1. A character array is a collection of variables which are of character datatype. String is a class that is instantiated to declare strings.
  2. Using index value you can access a character from a character array. On the other hand, if you want to access a particular character in a string, you can access it by function string’s_name.charAt(index).
  3. As an array is not a datatype similarly a character also is not a datatype. On the other hand, String being a class act as a reference type hence, it can be said String is a data type.
  4. You can not apply any operator on a character array whereas, you can apply operators on String.
  5. Being an array character array has a fixed length and its boundaries can be easily overrun. Where String does not have any boundaries.
  6. Array elements are stored in a contiguous memory location hence that can be accessed faster than string variable.

Conclusion

Inability to operate on character array raised the development of standard string class.

Related Differences:

  1. Difference Between One-Dimensional (1D) and Two-Dimensional (2D) Array
  2. Difference Between Array and Linked List
  3. Difference Between Array and Pointer
  4. Difference Between Stack and Queue
  5. Difference Between String and StringBuffer Class in Java

Comments

  1. santhosh s says

    November 24, 2022 at 4:42 pm

    good content.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Top 10 Differences

  • Difference Between OLTP and OLAP
  • Difference Between while and do-while Loop
  • Difference Between Guided and Unguided Media
  • Difference Between Preemptive and Non-Preemptive Scheduling in OS
  • Difference Between LAN, MAN and WAN
  • Difference Between if-else and switch
  • Difference Between dispose() and finalize() in C#
  • Difference Between for and while loop
  • Difference Between View and Materialized View
  • Difference Between Server-side Scripting and Client-side Scripting

Recent Addition

  • Difference Between Java and Python
  • Difference Between PHP and HTML
  • Difference Between GPS and GNSS 
  • Difference Between Virtualization and Containerization
  • Difference Between Storage and Memory

Categories

  • Artificial Intelligence
  • DBMS
  • Hardware
  • Internet
  • Networking
  • Operating System
  • Programming
  • Software

Copyright © 2025 · Tech Differences · Contact Us · About Us · Privacy