| | |
problem in strtok
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
>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.
New members chased away this month: 5
![]() |
Similar Threads
- problem with strtok() (C)
- strtok() problem (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
Views: 626 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






