Pointers and Arrays
eharetea

Pointers and arrays in C++.

Pointers and arrays.

Date Created:Friday December 29th, 2006 03:41 AM
Date Modified:Thursday July 31st, 2008 12:07 AM


#include <iostream>

using namespace std;

int main() { 

    int anMyArray[15];
    int i;
    
    for ( 015i++ ) {
    
        anMyArray[i] = rand()%100;
    
    }
    
        // these all work at declaration for declaring pointers
    //int *pnMyPointer;
        //int * pnMyPointer;
        intpnMyPointer;

    // THESE ARE THE SAME
    pnMyPointer = &anMyArray[0];
    // passing the array with no arguments gives you the memory location of the starting point    
    pnMyPointer anMyArray;

    for ( 015++ ) { 
    
        anMyArray[i] = anMyArray[i] + 1;
        cout << "anMyArray[" << << "]:" << anMyArray[i] << " ";
        cout << "pnMyPointer[" << << "]:" << pnMyPointer[i] << endl;    
        
        
    } 

    for ( 015++ ) { 
    
        // moves the memory location of pnMyPointer to the next memory location
        cout << *pnMyPointer++ << " is pnMyPointer++ or pnMyPointer[0]" << endl;
        
    }
    // returns to the original memory location
    pnMyPointer -= 15;
    
    return 0;

}

/*
RETURNS:
anMyArray[0]:84 pnMyPointer[0]:84
anMyArray[1]:87 pnMyPointer[1]:87
anMyArray[2]:78 pnMyPointer[2]:78
anMyArray[3]:16 pnMyPointer[3]:16
anMyArray[4]:94 pnMyPointer[4]:94
anMyArray[5]:36 pnMyPointer[5]:36
anMyArray[6]:87 pnMyPointer[6]:87
anMyArray[7]:93 pnMyPointer[7]:93
anMyArray[8]:50 pnMyPointer[8]:50
anMyArray[9]:22 pnMyPointer[9]:22
anMyArray[10]:63 pnMyPointer[10]:63
anMyArray[11]:28 pnMyPointer[11]:28
anMyArray[12]:91 pnMyPointer[12]:91
anMyArray[13]:60 pnMyPointer[13]:60
anMyArray[14]:64 pnMyPointer[14]:64
84 is pnMyPointer++ or pnMyPointer[0]
87 is pnMyPointer++ or pnMyPointer[0]
78 is pnMyPointer++ or pnMyPointer[0]
16 is pnMyPointer++ or pnMyPointer[0]
94 is pnMyPointer++ or pnMyPointer[0]
36 is pnMyPointer++ or pnMyPointer[0]
87 is pnMyPointer++ or pnMyPointer[0]
93 is pnMyPointer++ or pnMyPointer[0]
50 is pnMyPointer++ or pnMyPointer[0]
22 is pnMyPointer++ or pnMyPointer[0]
63 is pnMyPointer++ or pnMyPointer[0]
28 is pnMyPointer++ or pnMyPointer[0]
91 is pnMyPointer++ or pnMyPointer[0]
60 is pnMyPointer++ or pnMyPointer[0]
64 is pnMyPointer++ or pnMyPointer[0]
*/



Downloads:
Download: pointers.cpp 2 KB

Please login or Click Here to register for downloads
Creative Commons License
Pointers and Arrays 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