Increamenting Array Adresses

This is a discussion on Increamenting Array Adresses within the C++ Programming forums, part of the General Programming Boards category; Hi, i'm working with pointers and references and wanted to try out a simple program with an array. One variable ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    5

    Increamenting Array Adresses

    Hi, i'm working with pointers and references and wanted to try out a simple program with an array. One variable will store the adresse of the first element in the array, than - the array will be increamented to the next element, and thus will reassign the new adress to a variable. I thought this would be simple but, well its not =/.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    int main() {
    double a[100];
    double *pVar; //create pointer to variable
    pVar = &a[000]; //store adresse in pVar
    
       do {         //reiterate through untill done
         if (&a[001] < &a[099], ++&a[000]) {  //check if adresse of a[001] is less than next in array...
                  pVar = &++a[001]; //assign new adresse to pVar
                  cout << "adresse of a[???] = " << pVar; } //print new adress
    
                  else {  //when done...
                       cout << "done";
                       system("PAUSE"); }
                                        while(1); //loop while
    
                                        return(0); //return 0 to exit
                                        }
    }
    It is supposed to print the adresses of 99 array elements consecutavily. I'm not profficient in memory adresses so, please excuse me if this is a silly error...

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    For starters you aren't actually doing what you think you're doing. If what you want to do is cycle through each element of the array and assign it's address to another variable then a much cleaner way (since you know the size of the array before hand) would be like this:

    Code:
    #include <iostream>
    using std::cout;
    usind std::endl;
    
    int main() 
    {
    	double a[100];
    	double *pVar; //create pointer to variable
    	
    	for(int i=0;i<100;i++)
    	{
    		pVar=&a[i];
    		cout<<"address = "<<pVar<<endl;
    	}
    	cout<<"done"<<endl;
    	return(0); //return 0 to exit
    }
    Notice that I used the new standard iostream header, etc. You were also including stdlib.h even though you didn't need it.

    Also, why were you referencing elements with triple digits; ie: a[000], etc.?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21