Friday, January 13, 2012

Cplusplus: Understanding Arrays, Character Strings, and Pointers



I originally published this article on Yahoo Voices under the pen name John Mario.

We learned in earlier lessons that a variable is the name of a memory location that holds data. In computers, we also need a way to find that memory location. In real life, the US Post Office finds our residences via our address. Each memory location in a computer is assigned a numeric address.

Before we learn about pointers, arrays and strings, let's become familiar with the American Standard Code for Information Interchange (ASCII).

The ASCII code is a set of numbers that all keyboard characters are converted to. You can find the ASCII code at

http://www.asciitable.com/

When you press and release a key of your keyboard, the actual character shown on the key is not stored in memory. Instead, a number is stored in memory. That number is the ASCII code for that character.

The ascii code for the character 'a' is 141 in octal. The following program assigns the character 'a' to my_character and displays it. Then it assigns the ASCII code octal value 141 to my_character and displays it. The ASCII octal value 141 is displayed as the character 'a'

/**** place the include iostream statement here ****/

using namespace std;

int main (void)

{

/* Declare a character variable */

char my_character;

/* Assign my_character the letter 'a' */

my_character = 'a';

/* This statement displays the letter 'a' */

cout << my_character << endl;

/* Assign my_character the ASCII code for the letter 'a' */

/* The forward slash tells the compiler that the ASCII code is used instead of the letter. */

my_character = '\141';

/* This statement also displays the letter 'a' */

cout << my_character << endl;

} /* end of main function */

/******* End of program *******/

In C++, the ampersand sign means the address of when a variable name is appended to it.

&my_variable means 'the address of my_variable. A pointer points to a variable. In other words, a pointer holds the address of a variable.

The following program displays an integer in two ways. First it displays the integer via the variable my_variable which holds the integer. Then it displays the integer via the pointer my_pointer that points to my_variable.

**

/**** place the include iostream statement here ****/

using namespace std;

int main (void)

{

/* Lets declare a variable that holds an integer */

int my_variable;

/* Now lets declare a pointer*/

/*The asterick denotes a pointer to

a variable of type integer. */

int *my_pointer;

/* The pointer is declared but it does not point to any variable. Let my_pointer point to my_variable */

my_pointer = &my_variable;

/* Lets assign a value to my_variable */

my_variable = 5;

/* Display the integer stored in my_variable. */

cout << my_variable << endl; /* Displays the integer 5 */

/* Use the pointer my_pointer to display the integer in my_variable. */

cout << *my_pointer << endl; /* Displays the integer 5 */

} //end of main function

/******* End of program *******/

ARRAYS

An array is a group of contiguous memory locations holding a string of values. The following program creates an array of integers named my_array[2]. The number 2 means there are two locations in this array. Those locations are my_array[0] and my_array[1]. The program places integer values in each location and then prints them one at a time.

**

/**** place the include iostream statement here ****/

using namespace std;

int main (void)

{

/* The following statement declares an array of two memory locations designated as holding integers. Each location holds one integer. The two memory locations are my_array[0] and my_array[1] */

int my_array[2];

/* Assign the value 4 to the first location of my_array */

my_array[0] = 4;

/* Assign the value 3 to the second location of my_array */

my_array[1] = 3;

/* Display the contents of the first location of my_array */

cout << my_array[0] << endl;

/* Display the contents of the second location of my_array */

cout << my_array[1] << endl;

} // end of main function

/******* End of program *******/

The following program is the same as the last program except that the integers are assigned to my_array in the declaration of my_array.

/**** place the include iostream statement here ****/

using namespace std;

int main (void)

{

/* The following statement declares an array of two memory locations designated as holding integers.and assigns the value 4 to the first location of the array and the value three to the second location of the array */

int my_array[2] = {4, 3};

/* Display the contents of the first location of my_array */

cout << my_array[0] << endl;

/* Display the contents of the second location of my_array */

cout << my_array[1] << endl;

} // end of main function

/******* End of program *******/

We can also create an array of characters. The following program creates an array of characters. The array is named my_greeting. The program sets up a pointer to the array my_greeting and uses the pointer to display the greeting. The array of characters must be terminated with a null character. The null character denotes that all the characters have been printed.

**

/**** place the include iostream statement here ****/

using namespace std;

int main (void)

{

char *my_pointer;

/* declare an array that will contain five characters

and a null character. */

char my_greeting[6];

/* my_pointer must point to my_greeting */

my_pointer = my_greeting;

/* We can fill the array locations one by one with the following inefficient code */

my_greeting[0] = 'h';

my_greeting[1] = 'e';

my_greeting[2] = 'l';

my_greeting[3] = 'l';

my_greeting[4] = 'o';

/* We must add a null character to tell the compiler

that this is the end of the array */

my_greeting[5] = '\0';

/* Print only the first character of the array */

cout << *my_pointer << endl;

/* Print the whole array */

cout << my_pointer << endl;

} /* end of main function */

/******* End of program *******/

The above program could have been written using two C++ statements in the main function.

The following program is the same as the previous program except that it defines the characters in the pointer declaration. Note that the pointer declaration include the characters "hello." This is called a character string. When defining the character string in this manner, the programmer need not worry about the terminating null character.

/**** place the include iostream statement here ****/

using namespace std;

int main (void)

{

/* We could save time by filling the array in the declaration of the pointer */

const char *my_pointer = "hello"; /* generates compiler error */

/* "hello" is a character string that exists in the array my_greeting[]

my_greeting[0] is pointed to by the pointer my_pointer. */

/* Display the message "hello" */

cout << my_pointer << endl;

/* Print only the character 'h" */

cout << *my_pointer << endl;

} //end of main function

/******* End of program *******/

The following program uses the variable i as an index into my_array. Then it uses the pointer my_pointer to display the contents of my_array.

**

/**** place the include iostream statement here ****/

using namespace std;

int main (void)

{

/* declare integer i and set it equal to 0. */

int i = 0;

/* declare a pointer */

char *my_pointer;

/* Declare an array of two characters */

char my_array[3];

/* my_pointer must point to my_array */

my_pointer = my_array;

/* Use the value of i as the index into the array. */

/* Save the character 'H' in my_array[0] */

my_array[i] = 'H';

/* Increment i by 1 */

i++;

/* Save the character 'e' in my_array[1] */

my_array[i] = 'e';

i++;

/* Save a null character in my_array[2] */

my_array = '\0';

/* Display my_array */

cout << my_pointer << endl;

} /* end of main function */

/******* End of program *******/

We can also increment and decrement pointers.

The following program declares an array my_array and then uses the pointer my_pointer to point to specific locations of the array and store integers in the array.

**

/**** place the include iostream statement here ****/

using namespace std;

int main (void)

{

/* declare the index variable i */

int i = 0;

/* declare the character array my_array */

char my_array[3];

/* declare the pointer my_pointer */

char *my_pointer;

/* my_pointer must point to my_array */

my_pointer = my_array;

/* Store an 'H" in my_array[0] */

*my_pointer = 'H';

/* point to my_array[1] */

my_pointer++;

/* Store an 'e' in my_array[1] */

*my_pointer = 'e';

my_pointer++;

/* store a null character in my_array[2] */

*my_pointer = '\0';

/* Reset the pointer to the first location of the string */

my_pointer = my_array;

/* Display the character string in my_array. */

cout << my_pointer << endl;

} /* end of main function */

/******* End of program *******/

The following programs shows a property of the increment operator. The statement

my_array[i++] = 'T';

places the letter T in my_array[i] and then increments i.

The statement

my_array[++i] = 'T';

increments i and then stores the letter T in my_array[i].

After placing the characters 't', 'e', 's', 't' and a null character in the array. The program displays the word text.

/**** place the include iostream statement here ****/

using namespace std;

int main (void)

{

/* declare a character pointer */

char *array_ptr;

/* Declare a array of characters */

char my_array[7];

/* Declare and initialize the array index i */

int i = 0;

/* The pointer must point to the array *?

array_ptr = my_array;

/* store the character 'T' in my_array[i] and then increment i. */

my_array[i++] = 'T';

/* Store an 'e' in my_array[1] */

my_array[i] = 'e';

/* This statement increments i and then stores a the character 's' in my_array[i] */

my_array[++i] = 's';

i++; /*increment i */

/* This statement stores the character 't' in my_array[i] and then increments i. */

my_array[i++] = 't';

/* Insert a null character in my_array */

my_array[i] = '\0';

/* display the contents of my_array */

cout << array_ptr << endl;

} /* end of main function */

/******* End of program *******/

References: My own experiences during my career as a embedded systems software engineer.

The book 'C++ THE COMPLETE REFERENCE' by Herbert Schildt.

ISBN 0-07-222680-3


No comments: