What is the difference between a Character Array and a String?

In C these are almost the same, though a string will have an additional null character at the end.

In other languages (Java, C# etc), a string is an object, whereas a character array is an array of ... chars (which are primitive data types).

Normally, strings are implemented with character arrays.


I used to teach programming, and this is how I used to explain this particular issue.

First, focus on what both things have in common: both a char array and a string consist of a sequence of characters. Being a sequence implies that the characters are ordered and that they can be enumerated, for example.

Now focus on what each of the two things add, in their particular different ways, to this common ground.

A char array adds what any array is known to add: indexing and random access to individual items.

A string, on the other hand, adds the fact that the sequence of chars is seen as a whole thing with its own properties. In some implementations, achieving this means altering the way the chars are stored (adding a terminating null in C strings, for example).

This approach (look at the commonalities, then at how things diverge from them) has proven useful in a variety of situations.

Hope this helps.


A String is an abstraction, but of a sequence of characters. It says nothing of implementation. If you wanted to create a String implementation based on a linked list of characters there's nothing stopping you.

In a language such as C, there is very little difference - just that a c string is a null-terminated series of characters at sequential addresses, that is generally accessed through a pointer.

In an OOP language, a String will be an object of some String class. This will probably hold the data in a character array internally, but you don't need to know that. A character array can only be a simple array, but a String class can provide many operations (substrings, regex, etc) on strings if the implementer decides to.


a character array is simply an array of characters

a string is data structure that uses an array of characters

some string representations use a null-terminator (like C), others use a length prefix

Tags:

Paradigms