README.md
December 4, 2022 ยท View on GitHub
Description
An Example on the Usage of Pointers
More Info
Always be careful to know the exact location in memory pointers are aiming at before changing the values they point to.
| Submitted On | |
| By | Ty |
| Level | Beginner |
| User Rating | 4.3 (34 globes from 8 users) |
| Compatibility | C, C++ (general), Microsoft Visual C++, Borland C++ |
| Category | Coding Standards |
| World | C / C++ |
| Archive File |
Source Code
//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
*/