Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: C++
Expertise: Intermediate
Jan 18, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Iterators Aren't Pointers

Suppose you define a the following list object and you need the address of one of its elements:

  std::list <int>  li;  
  std::list <int>::iterator  iter = li.begin();

In many contexts, iter functions like a pointer. However, when you need a real pointer to a container's element, you can't use an iterator:

  int func(int * p);  
  int main()
    func(iter); // error, iter is not a pointer to int

The problem is that in general, iterators aren't pointers. Rather, they are usually implemented as objects. To get a pointer to the element an iterator refers to, you need to "dereference" that iterator and take the address of the result. For example:

  int main()
    func( &*iter); // ok
Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date