VB icon

A Beginner's Guide to Pointers

Email
Submitted on: 1/2/2015 8:17:00 PM
By: Ty (from psc cd)  
Level: Beginner
User Rating: By 8 Users
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++
Views: 1872
 
     An Example on the Usage of Pointers
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: A Beginner's Guide to Pointers
// Description:An Example on the Usage of Pointers
// By: Ty (from psc cd)
//
// Side Effects:Always be careful to know the exact location in memory pointers are aiming at before changing the values they point to.
//**************************************

//Free Source Code Tutorials Free for the taking
//Blazon Resources
//http://www.members.home.net/blazonres
//
//Email at blazonres@home.com
#include <iostream.h>
void main()
{
	int MemoryAddress = 0;	//Value in memory to point to
	int *Pointer;			//Stores(Points to) value of memory location
	Pointer = &MemoryAddress;	//Point to the address of 'MemoryAddress'
	//No '*' used here because it points to the location of 'MemoryAddress'
	//Pointer now knows where the value of 'MemoryAddress' is and
	//therefore can be used to change it (as used here from 0 to 1)
	cout << MemoryAddress << endl;	//Show the value of 'MemoryAddress'
	*Pointer = 1;	//Point to the location in memory and change the value to 1
	cout << MemoryAddress << endl;	//Show the new value of 'MemoryAddress' on the screen
}
/*
NOTE:	
Changing the value of an unknown memory location may cause the computer
to crash or lose vital information causing serious problems.
Ommitting the line:
"Pointer = &MemoryAddress;"
will cause the program to change a nearly random location in memory which
could cause serious damage to the computer. So be sure to define the location
in memory that is intended to be changed
*/


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.