Pointers 


Contents of this section:

  1. A simple program using pointers
  2. Pointer to characters
  3. Pointer arithmetic
  4. Array of pointers

1. A simple program using pointers

    Code Listing: 

// File name: ~ftp/pub/class/cplusplus/Pointer/PointerSimple.cpp
// 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:

mercury[5]% CC -LANG:std PointerSimple.cpp -o PointerSimple
mercury[6]% PointerSimple
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:

// File name: ~ftp/pub/class/cplusplus/Pointer/PointerChar.cpp
// 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:

mercury[156]% CC -LANG:std PointerChar.cpp -o PointerChar
mercury[10]% PointerChar
Original content of Buffer:--> Dummy content.
Enter a sentense: 
This is my sentense!
Now the Buffer contains:--> 
This is my sentense!
mercury[11]% 

3. Pointer Arithmetic

Code listing:

// File name:  ~ftp/pub/class/cplusplus/Pointer/PointerArith.cpp
// 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:

mercury[19]% CC PointerArith.cpp -LANG:std -o PointerArith
mercury[20]% PointerArith
1 2 3 4 5 6 7 8 9 10 
mercury[21]% 

 

4. Array of Pointers

Code Lising

// File name:  ~ftp/pub/class/cplusplus/PointerArray.cpp
// 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

mercury[121]% CC -LANG:std PointerArray.cpp -o PointerArray
mercury[122]% PointerArray
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.
mercury[123]% 
 

Last modified: Friday, 21-Aug-2020 15:28:15 CST
Copyright 2000 Department of Computer Science, University of Regina.

 CS Dept Home Page

Teaching Material

C++ Index Page