how to convert char to char * ?
ive got:
char * string;
char string2[10];
and i want to make string = string2.
but when i try things like strcpy eg...
this is orginal text:
"hi there matie s"
and after the conversion i get...:
"hi there matie sÇ ↕"
is there any proper way of converting it?
you need to zero terminate your strings. when u copy from the array you''re getting all the garbage at the end copied too.
try adding ''\0'' at the end of the string. NOTE: you can get the length of the string using strlen().
I hope this helps some.
-Boblin
I hope this helps some.
-Boblin
char * string = NULL;
char string2[10] = {0};
try that. it should work.
and that''s why in programming class they tell u to initialize your variables, especially your pointers/arrays.
then you don''t things like what you got
char string2[10] = {0};
try that. it should work.
and that''s why in programming class they tell u to initialize your variables, especially your pointers/arrays.
then you don''t things like what you got
Maybe strdup() - or something like it - will work out.
It allocates memory for a string and returns the pointer to it.
char *string = 0;
char src[] = "MyString";
string = strdup( src ); // not sure if this would compile, but it should work.
delete[] string; // delete the stringmemory
string = 0;
--
You''re Welcome,
Rick Wong
- sitting in his chair doing the most time-consuming thing..
It allocates memory for a string and returns the pointer to it.
char *string = 0;
char src[] = "MyString";
string = strdup( src ); // not sure if this would compile, but it should work.
delete[] string; // delete the stringmemory
string = 0;
--
You''re Welcome,
Rick Wong
- sitting in his chair doing the most time-consuming thing..
quote:Original post by johnnyBravo
thanks pipe, it works beautifully!
Errrr.. pipe?? Hehehe.. no problem.
--
You''re Welcome,
Rick Wong
- sitting in his chair doing the most time-consuming thing..
quote:Original post by johnnyBravo
c++
Then ditch those unsafe, error prone char* and go the proper C++ way: std::string. Using char* for strings in C++ is just plain stupid (or ignorant, take your pick ).
std::string can and will hide all these problems from you, and prevent you making any silly mistakes.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement