Ryan Bartley
Ryan Bartley

Reputation: 616

Finding the individual addresses of data linked by a linked list. C++

I'm having a bit of trouble getting some addresses back from the items located in a linked list. Instead of the iterator address created and initialized at the beginning of the for loop, I'd like to get at the address of the actual data located inside the list. Is this possible without creating my own implementation of lists? Here is the code block that I've gotten to but clearly it's not working the way I want it to.

int j = 0;

testIntList.resize(100);
for (list<int>::iterator i = testIntList.begin(); i != testIntList.end(); ++i) {
    *i = j*j;
    int * k = &(*i);
    cout << "the value of the iterator is " << *i << " and the address of the iterator is " << &i << "and the address of the pointer is " << *k << endl;
    j++;
}

I'm using k in this way to try and get at the actual pointer in memory of the address of the value of the iterator. Any help would be very much appreciated.

Upvotes: 0

Views: 481

Answers (1)

Joseph Mansfield
Joseph Mansfield

Reputation: 110658

i is your iterator, so &i would be the address of the iterator - not what you want. Now, you know that *i is the object that your iterator points at, so the address of that object is &*i. That's what you've put in k. Just print out k.

cout << "the value of the iterator is " << *i
     << " and the address of the iterator is " << &i
     << " and the address of the pointer is " << k << endl;

Upvotes: 2

Related Questions