More pointers in C++.
C++ pointers.
Date Created:Friday December 29th, 2006 03:41 AM
Date Modified:Wednesday July 30th, 2008 11:59 PM
#include <iostream>
using namespace std;
// *a is the value of the memory location
void swap(int *a, int *b) {
int temp = *a;
*a = *b; // *a will change the value of the actual memory location that this value origins from.
*b = temp; // using *b is actually like using the variable its pointing to.
}
int main() {
int x = 5;
int y = 6;
cout << x << " is x " << endl << y << " is y " << endl;
swap(&x,&y);
cout << x << " is x " << endl << y << " is y " << endl;
return 0;
}
/*
This demonstrates what pointers are about.
Memory Location Name Value
0x10 x 5
0x33 y 6
0x92 a(*) 0x10
0x99 b(*) 0x33
& gives you the memory location of a variable:
&x = 0x10
* gives you the memory location of the variable the pointer is referencing:
*a = 0x10
*/
Downloads:
Download: more_pointers.cpp 944 B
Please login or Click Here to register for downloads
More Pointers by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at www.3daet.com
Permissions beyond the scope of this license may be available at http://www.3daet.com
