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

Recommended Answers

All 2 Replies

use char arrays instead of char pointers.

commented: Yes indeed! +26
commented: Fixes the problem but teaches nothing in the process. -6

>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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.