Basic Data Types

Data refers to items of information, which in our context, may be stored in the memory of the computer. The distinction between Data and Information is critical in the minds of Information Systems people. However, as far as this course is concerned, the difference can be ignored. Any piece of information, whether processed or not, can constitute data.

Data can be stored in the memory of the computer, associated with variables or constants. The following are all examples of data.

Example :
int x=12, yy;
char aString[]="Hello", *p;

While x and aString have associated items of information, yy and p dont. All of the names declared above are names of variables which can contain data.

The basic data types are integers, real numbers, characters, enumerated data, pointers, arrays and records.

All data can be operated upon by standard built-in operators and functions. The set of such operators and functions is different for each data type. The data would itself be meaningless if not for the associated operators and functions. Thus, the data together with its operators and functions constitutes a complete programmatic unit. If the same or equivalent sets of functions could be used for every data type, then the actual data type would not be as important as the functions - the data type could be hidden from the user. The data would be operated upon only through a defined set of functions and not directly - allowing freedom of internal representation. This is known as the Abstraction of Data.

Any piece of data can be considered incomplete if not for its set of associated operators or functions. On its own it has no meaning. However the introduction of an operator (eg. +) gives meaning to the data and makes it useful. This completeness of data implies that any study of data has to be accompanied by a study of the associated operators.

In the following discussion, it is assumed that there is no difference between operators and functions. For example, the addition operator could just as easily be replaced by a function which adds its two parameters and returns the sum.

Integer

Integers are declared with the keyword int.

Example:
int x;

Integers are whole numbers with different ranges depending on the type modifiers (long, unsigned).

The following functions can be applied to integers:

Real Numbers

Real numbers are declared with the keyword float or double (for greater precision).

Example:
float x=1.23;

Real numbers have the same operations as integers, with the exception that there is no modulus (5) operation.

Characters

Characters refer to viewable letters or symbols, but in C++ they are simply numbers in the range 0-255 (or -128 to 127). They are declared with the keyword char.

Example:
char aChar;

Enumerated Types

These refer to the data that can take on values from a pre-defined set.

Example:
enum Names {John, Tom, Mary, Susan};

Enumerated data only allows assignment and relational operators.

Pointers

Pointers are associated with memory regions which contain data. They are denoted by prefixing a normal variable declaration with *.

Example:
int j, *s = &j;

Pointer operations are generally different from other data types because pointers do not contain the data but rather contain references to the data. The operations are as follows:

Records

Records are compositions of data items for easier storage and manipulation. Data items of differning types can be stored within a record, defined with the struct operator.

Example:
struct {
   char Name[100];
   int Age;
} X;

Records have only one operator - the access operator ".". This allows the user to access an individual field of the record. For example, using the above declaration, X.Age refers to the second field of the record. The individual data items can then be operated upon by the relevant functions.

Arrays

A number of the same type of data item can be stored sequentially in memory and accessed through a single variable. This is called an array. Arrays can be composed of any data type but the length of the array must be fixed at compile-time (unless it is dynamically allocated).

Example:
int IntegerArray[10], 2DArray[10][10];

Arrays also have only a single operator - the index operator "[]". Just like records, this single operator allows the user to isolate individual elements of the array. For example, IntegerArray[0] would be used to isolate element number 0 of the array declared above.

Arrays are implemented internally as pointers to the first character, therefore they can be treated as pointers and interchanged in some contexts.

Strings

Although not a pure data type, strings are considered a separate data type because of the strict syntax - namely, every string must be terminated by a null character. This strict requirement necessitates a set of functions to manipulate strings. In C++ these functions are numerous and not implemented as operators since a string is not a primary data type.

Example:
char CharArray[100];
char *CharPointer;

Strings must be stored in character arrays. The various functions that operate on strings automatically add the terminating null character. When passed as a parameter to a function, the string may be passed as a pointer to the first character - a character pointer. This makes passing of parameters faster. Explicitly, a character pointer can also be made to point to a character array (but not the other way around).

Applications of Arrays

Indexing

Besides the obvious storage of data, arrays indiced can also be used to increase the efficiency and elegance of code. The example below indicates a more elegant method of converting decimal to binary without the need for repeated division and inversion of the numbers for proper display.

Example:
int Num=73;
int Mask[]={128,64,32,16,8,4,2,1};
for (int a=0; a<8; a++)
   cout << (Num & Mask[a]) / Mask[a];

Matrices

A simple array is analagous to a 1-dimensional vector. Two-dimensional arrays can be used to store matrices. However, there are times when the dimensions of the matrix are not known in advance. In such cases, either a block of memory can be allocated or a large 1-dimensional array can be pre-declared. The conversion to two or more dimensions will have to be done programmatically.

For a 1D array :
Position = Start + (Index * SizeofDataItem)

For a 2D array :
Position = Start + [(Row * NumberofColumns) + Column] * SizeofDataItem

For a 2D array based at (1,1) :
Position = Start + [(Row-1)*NumberofColumns + (Column-1)] * SizeofDataItem

Stacks

A stack is a linear collection of data items, such that the list of data can be accessed from one end only. Specifically, data items can only be added, removed or one end. It is called a stack because of the analogy to a "stack" of physical items, where new items are added to the top and existing items can only be removed from the top of the stack.

A stack is an example of an advanced data type and will be studied in greater detail in later chapters.

An array can provide a means to implement a stack by storing data in sequence from one end of the array. A Stack Pointer can point to the next position where an element may be stored. Then, when adding an element, the data is stored at the Stack Pointer and it is advanced one position. When an item is removed, the Stack Pointer is decreased by one. Because of the array implementation, any changes to the stack must also take into consideration the bounds of the array. So, when adding an element, the user must first check that the array isn't full and when removing an element the user must check that the array isn't empty.

See Program DS01 for a sample application of a stack.