A primer for pointers in C++.
Pointers C++.
Date Created:Friday December 29th, 2006 03:41 AM
Date Modified:Wednesday July 30th, 2008 11:55 PM
#include <iostream>
using namespace std;
int main() {
const int nMyArraySize = 6; // const makes it so this variable cannot change.
int nMyArray[nMyArraySize]; // computer needs to know how big array is.
for (int i = 0; i < nMyArraySize; i++) {
nMyArray[i] = rand()%100;
cout << &nMyArray[i] << " is nMyArray[" << i << "]'s location in Memory" << endl;
}
int y = 54;
int x = 6;
int* pMyPointer;
pMyPointer = &x;
*pMyPointer = 7; // this changes x because its pointing to x
pMyPointer = &y;
*pMyPointer = 7;
cout << x << " " << y << " " << *pMyPointer << endl; // data
cout << pMyPointer << " " << &y << endl; // data location
return 0;
}
Downloads:
Download: pointers.cpp 749 B
Please login or Click Here to register for downloads
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
