0

Hi

I am writing a code in VC++

char* style = "color:#4D4DFF;font-size:9pt;font-family:Arial;";
char* semicolonsplit = "";
semicolonsplit = strtok (style,";");
But in the last line I am getting an error

" Access violation writing location 0x0041606d."

Am I missing something.

Regards
Karan

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by Narue
0

>Am I missing something.
Yes. iDeveloper gave you the fix, but the reason for it is that you're initializing pointers to string literals. The problem isn't the pointers per se, it's the initialization of a pointer to a string literal. A string literal in C++ is defined as an array of const char, and strtok modifies the first argument by writing null characters at each delimiter. The access violation comes from strtok trying to write to read-only memory.

By changing style from a pointer to an array, the initialization changes as well. Instead of assigning the address of the string literal to a pointer, you're copying the contents of the string literal to a non-const array that you own and can modify.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.