Hi,

I am trying to overcome this simple yet confusing problem. I have an array of structures with one char* member. I have used itoa() to convert an integer to char* and stored this in the structure. The problem I have is that after the array of structures has been initialized, all the elements in the array seem to contain the last set value!! This can be seen by running the following program



#include <iostream.h>
#include <stdlib.h>

void main(int argc, char* argv[])
{
struct test
{
char* name;
};
test * structArray = new test[4];
char *temp = new char[4];
for ( int i = 0; i< 4; i++)
{
itoa(i,temp,10);
structArray[i].name = temp;
cout<< structArray[i].name ;
}

cout<< structArray[0].name ;
cout<< structArray[1].name ;
cout<< structArray[2].name ;
cout<< structArray[3].name ;

}