|
-
January 4th, 2002, 06:32 AM
#1
char* array confusion
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 ;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|