Technology Advances

Pointers

1. A simple program using pointers

    Code Listing: 

// Purpose: A simple program demonstrating the use of pointers.

#include <iostream>
using namespace std;

int main()
{
// declare an integer and a float variable
int IntNum;
float FloatNum;

// declare integer and float pointers
int *pIntNum;
float *pFloatNum;

// initialize the integer and float variables
IntNum = 10;
FloatNum = 12.34;

// store addresses in pointers
pIntNum = &IntNum;
pFloatNum = &FloatNum;

// print out the original values
cout << "Before increment: " << endl;
cout << "\t IntNum is: " << IntNum << endl;
cout << "\t FloatNum is: " << FloatNum << endl;

// note that we need to dereference a pointer in order
// to extract the value it contains.

cout << "\t pIntNum contains: " << *pIntNum << endl;
cout << "\t pFloatNum contains: " << *pFloatNum << endl;

// increment values of the integer and float variables
(*pIntNum)++; // dereference and then increment
(*pFloatNum)++;

// print out the values after increment
cout << "After increment: " << endl;
cout << "\t IntNum is: " << IntNum << endl;
cout << "\t FloatNum is: " << FloatNum << endl;

cout << "\t pIntNum contains: " << *pIntNum << endl;
cout << "\t pFloatNum contains: " << *pFloatNum << endl;

return 0;
}

    Running session:

Before increment:
IntNum is: 10
FloatNum is: 12.34
pIntNum contains: 10
pFloatNum contains: 12.34
After increment:
IntNum is: 11
FloatNum is: 13.34
pIntNum contains: 11
pFloatNum contains: 13.34

 

2. Pointer to characters

Code listing:

// Purpose: demonstrate the use of pointer to char

#include <iostream>
#include <stdio.h> // use <cstdio> for newer compilers.
using namespace std;

int main()
{
// a pointer to char is initialized with a string literal
char *Buffer = "Dummy content.";

cout << "Original content of Buffer:--> " << Buffer << endl;
cout << "Enter a sentense: " << endl;

// use gets() to get the whole line.
// cout would only get the first word.

gets(Buffer);
cout << "Now the Buffer contains:--> " << endl;
cout << Buffer << endl;
return 0;
}

Running session:

Original content of Buffer:--> Dummy content.
Enter a sentense:
This is my sentense!
Now the Buffer contains:-->
This is my sentense!

 

3. Pointer Arithmetic

Code listing:

// Purpose: Demonstrates pointer arithmetic

#include <iostream>
using namespace std;

int main()
{
int IntArray[10]; // array of int
int *pInt; // pointer to int
int i; // loop counter

// populate the int array with 1, 2, 3, ...
for(i = 0; i < 10; i++)
{
IntArray[i] = i+1;
}

// store the address of the first element of IntArray
//in pointer

pInt = IntArray;

// print out values in the int array
for (i = 0; i < 10; i++)
{
cout << *pInt << ' ' ;
pInt++; // pointer arithmetic
// the above two lines could also be replaced with:
// cout << *(pInt++) << ' ' ;

}
cout << endl;
return 0;
}

Running session:

1 2 3 4 5 6 7 8 9 10

 

4. Array of Pointers

Code Lising

// Purpose: Demonstrates the use of array of pointers.

#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
// initialize an array of char pointers with five fortunes
const char* const Fortune[5] =
{" Earth is a great funhouse without the fun.",
" There is always more hell that needs raising.",
" Confession is good for the soul, but bad for the career.",
" Live in a world of your own, but always welcome visitors.",
" The man who laughs has not yet been told the terrible news."
};
cout << "Here are the 5 fortunes: " << endl;
for(int i = 0; i < 5; i++)
{
cout << Fortune[i] << endl;
}


}

Running Session

Here are the 5 fortunes:
Earth is a great funhouse without the fun.
There is always more hell that needs raising.
Confession is good for the soul, but bad for the career.
Live in a world of your own, but always welcome visitors.
The man who laughs has not yet been told the terrible news.