Arrays, Strings and Pointers


1. Arrays

So far, we have covered all the fundamental data types and you have a basic knowledge of how to perform calculations and make decisions and loops in programs. Now we will take a look at arrays, strings and pointers.

1.1.   Arrays

Let’s say that you need to write a program which process the results of some experiment and let’s say that you have 1000 data values. For labeling and handling this equivalent data you would need a lot of patience and time. If we use special names for each data, the program would have to consist of 1000 identical blocks of commands that would only differ in the name of the variable in the respective block processed. The inefficiency of this approach is obvious. Far more advanced would be if all the data is stored under the same terms and the specific result is retrieved using a numerical index. Such data processing is allowed using arrays.
An array is a collection of variables of the same type that are referred to by a common name. In C++, arrays may have from one to several dimensions, although the one-dimensional array is the most common. Arrays offer a convenient means of grouping together several related variables. The array that you will probably use most often is the character array because it is used to hold strings. As explained earlier, the C++ language does not define a built-in string data type. Instead, strings are implemented as arrays of characters. This approach to strings allows greater power and flexibility than are available in languages that use a distinct string type.
Share on Google Plus