TITLE: converting STL list to built-in array (Newsgroups: comp.lang.c++, 21 Oct 98) GREENFIELD: Pavel Greenfield > >Is there an built-in way to convert a list to an array (T *)? PHLIP: Phlip > What about this? > > T anArray [anArrayEXTENT]; > list::iterator it (yourList.begin ()); > > copy (anArray, anArray + anArrayEXTENT, it); > > I don't feel like compiling it, so I turned the Subject line back into a > question. KOENIG: ark@research.att.com (Andrew Koenig) This example has a common bug: Generic algorithms such as copy do not act on containers, but only on container elements. Therefore, they cannot change the size of the destination container. Presumably yourList doesn't have the right number of elements to begin with, so the call to copy will run off the end of the list. If you want to use copy this way, do something like copy (anArray, anARray + anArrayEXTENT, back_inserter(yourList)); Of course, this doesn't answer the original question, which was how to go in the other direction. Here's an example: T* array_ptr = new T[yourList.size()]; copy(yourList.begin(), yourList.end(), array_ptr);