Category: 

What Is a Char Array?

Article Details
  • Written By: Eugene P.
  • Edited By: Angela B.
  • Last Modified Date: 30 November 2016
  • Copyright Protected:
    2003-2016
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
Helium is the only element that was discovered in space before it was found on Earth.  more...

December 10 ,  1948 :  The UN adopted the Universal Declaration on Human Rights.  more...

An array of characters is commonly known in most computer programming languages as a char array. This is primarily because "char" is the keyword in languages such as C that is used to declare a variable of the scalar character data type. A char array is a sequence of characters recorded in memory in a long line of consecutive addresses that can be quickly accessed by using the index of an element within the array. Like many arrays of scalar data types, the benefits of using a char array are to allow for fast random access and for replacement of values at arbitrary positions within the array.

In object-oriented programming languages such as Java®, it is important to understand that a char array represents a sequence of scalar values and not classes or objects. A scalar data type is one that is stored only as a piece of datum, like an integer. This is in contrast to an array of character objects, which contains methods and possibly other supporting variables such as serialization or synchronization information.

Ad

A character in most computer languages is a data type designed to hold a single letter or symbol as it is represented by the character encoding used by the program or operating system. For this reason, except in strongly standardized languages, the actual size of a character in memory might be different on each system. This information can make a difference when attempting to traverse a char array using pointer arithmetic, where instead of using simple increment and decrement operators, pointer calculations need to use the system-defined character size. Relying on an array in which the character size is 1 byte or 2 bytes can cause unpredictable behavior and errors if the program is run on another system in which a character could be defined differently.

Within some programs, an array of characters could be used as the basis for the implementation of a string class or advanced string data type. In the C programming language, strings are actually handled in a way very similar to a char array in which a consecutive sequence of characters forms the string, informing the program that the string has ended with a null character. A custom string class could be designed to emulate this behavior with an array.

Other uses for a char array include performing types of compression or encoding that is not necessarily based on octets but on human readable characters. The array also could serve as a type of buffer for the processing of longer blocks of text that are held in a physical media file, or one that is being read in from a network socket. Most of the functions for which a char array is used involve text input and output simply because the size of a scalar character is unpredictable from one system and compiler to another in many languages and, so, cannot be relied on to hold integer values beyond a certain range.

Ad

You might also Like

Recommended

Discuss this Article

Post your comments

Post Anonymously

Login

username
password
forgot password?

Register

username
password
confirm
email