problem in strtok

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 46
Reputation: karang is an unknown quantity at this point 
Solved Threads: 0
karang karang is offline Offline
Light Poster

problem in strtok

 
0
  #1
Jan 2nd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 49
Reputation: iDeveloper is an unknown quantity at this point 
Solved Threads: 7
iDeveloper iDeveloper is offline Offline
Light Poster

Re: problem in strtok

 
0
  #2
Jan 2nd, 2009
use char arrays instead of char pointers.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 707
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: problem in strtok

 
0
  #3
Jan 2nd, 2009
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC