Possible Duplicate:
In C++, Why can't I write to a string literal while I can write to a string object?
I'm having my first experience with wchar_t strings and I'm having a lot of trouble. Each time I try to access a character in an wchar_t* the program crashes with segmentation fault. How should I do if I want to replace one character in the string with another? But when deleting a character from the end of the string?
wchar_t * my_string[] = L"Hello";
my_string[0] = L'Y'; // Should be "Yello". Instead, gives segmentation fault
[edit] Doesn't matter, I've just made a fool out of myself. I'll check elsewhere on the internet. It's my fault, I shouldn't be bothering you with such silly questions...
You didn't supply much information, but, since you talk about segmentation fault, the most common errors derived from the fact that memory management functions usually works on bytes while wchar_t
has a size greater than 1.
When doing pointer arithmetic over char, the sizeof(char)
is almost never taken in count, since it is 1 by definition. But wchar_t
is wider, hence where bytes length are required, a multiplication of sizeof(wchar_t)
must be placed.
[EDIT] Sorry but the sample has nothing to do with wchar_t
itself:
wchat_t* my_string[]
is an arry of wchar_t-pointers, the first of which is made to point to "Hello", and the other are left uninitialized. Just remove the *.