| | |
problem in strtok
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
>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.
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.
![]() |
Similar Threads
- strtok() problem (C)
- problem with strtok() (C)
- Problem with strtok() for data structure creation (C++)
- Search string in a text file (C)
- int problem (C++)
- Problem with a snippet of code in a shell program (C)
- Strtok() (C)
Other Threads in the C++ Forum
- Previous Thread: quick synax question
- Next Thread: C++ Problem about Classes
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






