STL (URGENT Please) question...

Hi...
This is a easy question about Standard Template Library(STL). I have used "vector" class of type "char *" in my program. I have appended below my complete program (run in g++ compliler). Here v1 is a vector of type "char *". I have pushed (using the method push_back) in v1 a variable "vtop" (of type "char *"). Initially the variable "vtop" contains "EECS171". Now, when I print th econtent of the vector v1, I get that its only content is "EECS171". That is alright...
But now I assign (after I pushed_back to the vector v1) a new string of char * to the variable vtop. Say the new char string is "EECS370". Now when I print the content of the vector v1, surprisingly I find that its only content is "EECS370" (NOT "EECS171" !!!). I wonder who the vector v1 gets changed when I only modified the content of the variable "vtop"...
Here is the program...
If you have any idea of why this is happening, pls let me know....
Thanks a lot...
sunanda
My complete program:                                                                      
#include<iostream.h>
#include<string.h>                      #include<vector>                                
using namespace std;                                                            
                                                                                 
void printVector(const vector<char *> &vec);                                    
main()                                                                          
{                                                                                
vector<char *> v1; //Stores POSTFIX string                                      
                                                                                 
char *vtop;
vtop = new char [20];
strcpy (vtop, "EECS171");                                              
v1.push_back(vtop);                                                              
strcpy(vtop, "EECS370");  //This above line surprisingly changes the vector v1 !!!!!                                        
                                                                     
cout<<"v1 is: ";                                                    
printVector(v1);                                                    
                                                                     
}                                                                    
                                                                     
void printVector(const vector<char *> &vec)                          
{                                                                    
vector<char *>::const_iterator p1;                                  
for (p1=vec.begin(); p1 != vec.end(); ++p1)                          
cout<<*p1<<' ';                                                      
cout<<endl;                                                          
}                                                                    
                                                                                                                                               
skunduAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yowkeeCommented:
There is no surprising. You store the char* into a vector, then what the container contain is a pointer which point to an char array.

eg, vtop (0x400000a0) --> "EECS171"

   store vtop to v1, so
   v[0] = 0x400000a0

   modify, vtop -->  "EECS370"

   now, 0x400000a0 point to "EECS370",
so it *v[0] will print "EECS370".

0
yowkeeCommented:
For a better choice and less confusion, use vector<string> instead of vector<char *> .
0
nietodCommented:
yowkee's explanation is a little hard to undetstand (for me at least), but it is correct.  You are storing a pointer to a string, not the actuall string.  If the string stored in that location changes, then the pointer to the string points to that changed string.

His solution is 1 of many posibilities, but is probably the best one.  In fact you should usually avoid using the C-style "char *" strings wherever possible.  Your should probably switch to the STL string class or another string class for storing all strings.  This will make your program safer, more efficient and easier to write and maintain.

yowkee, it seems to me that you have answered the question.  You should post an official answer.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

yowkeeCommented:
nietod, I have to learn more on how to express things from you 8-). You provide a good guide on avoiding char pointer.

skundu, it seem it's really urgent for you, so you post another question for char* --> string. You could follow this question if you have any problem with it. 8-)

Answer as my comment. (wondering is there a feature to post previous comment as answer....)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
>> is there a feature to post previous comment as answer
No, but it doesn't matter.  The whole question history is saved in the database.
0
yowkeeCommented:
Thanks :)
0
skunduAuthor Commented:
Hi...
Thank you for your answers... I thought that the char * is stored in the vector's own memory space, actually that is not...
So I should try with string class...
Thanks again...
sunanda
0
nietodCommented:
I hate to confuse you...but hopefully this will straighten you out.

The "char *" is stored in the vector.  That "char *" is not a string, although programmers somethimes call it a string.  It is a pointer to a string.   So the vector<> does not store the string, it stores a pointer to a string that is stored elsewhere.  The problem with that is that the "elsewhere" might be a place that is volatile.  i.e. that elsewhere place may change its contents.  i.e. change the string it stores.  the char * pointer wil continue to point to this place, but now this place might store a new string.  For example.

char Str[] = "abcde"; // This really is a string.
char *StrPtr = Str; // This is just a pointer to the string elsewhere.

strcpy(str,"12345");  // This changes Str, but it also then appearss to change StrPtr, because StrPtr points to the string stored in Str.
0
skunduAuthor Commented:
Hi Nietod,

Thank you very much for your above explanation. I greatly appreciate it. It is now clear to me where I made mistake.......
I hope to get rid of my problem by using string class object,
instead of using char*....
Thanks a lot...
sunanda
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.