Showing posts with label address. Show all posts
Showing posts with label address. Show all posts

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


How Your Conputer Uses Memory

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

Most new computer has several gigabytes of memory. A gigabyte is 1,000,000,000 bytes. This article talks about how your computer keep track of where everything is stored in this memory. You don't need a background in electronics to read this article. It is an article for those who want to learn about computer memory without getting buried in a myriad of complicated technical terms.

Let's digress for a moment. Each family that lives on any given street has a door number on their front door. That door number, as you well know, is part of your address.

Every memory location has an address associated with it. For purposes of this discussion, a memory location is defined as one byte of memory. A byte can hold an 8 digit binary number. In decimal, this means a byte can hold any number from 0 to 255.

When the computer program wants to write data to memory, it accesses memory via the memory address. As you learned in my article on programming, the programmer assigns a name to a memory location. The name describes what is in that memory location. That name is associated with a memory address. Lets assume the name is price_of_apple and the memory address is 36125. Now we write an instruction in our program to save the value 0.95 in the memory named price_of_apple. When that instruction is executed, the memory location with the address 16125 will contain the value 0.95. The English explanation would be that an apple costs $0.95.

Lets take a closer look at what is happening inside your computer without getting buried in mounds of technical jargon:

Basically there are two different types of memory: sequential access and random access. In sequential access memory, you must read everything that is in memory in the order that the data appears until you find the data you are looking for.

In random access memory, you can read any memory locations you want without having to read the other locations. This article will focus on random access memory.

A memory stick inside my HP computer looks like a thin rectangular piece of plastic with multiple pins on one edge. Inside my computer is an Intel computer chip. This Intel chip is referred to as a processor. When the computer program wants to store the price of an apple in memory, the processor sends that price, 0.95, to the memory stick. The processor also sends the address 36125 to the memory stick. Then the processor sends a command to the memory stick telling it to save the price at the memory address. Hence the price is written to the memory location whose address was sent by the processor.

Now let's talk about reading a memory location. When the computer program wants to read the price of an apple, the processor sends the address (36125) of the memory location price_of_apple to the memory stick. Then the processor commands the memory stick to send the price back to the processor. The memory stick sends the price (0.95) to the processor.

This concludes the explanation of how your computer uses memory.

References

I have a Bachelor of Science degree in Electrical Engineering and worked as an embedded systems software engineer.