Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8833645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:49:48+00:00 2026-06-14T08:49:48+00:00

I’ve always understood that strtok should be regarded with caution due to the fact

  • 0

I’ve always understood that strtok should be regarded with caution due to the fact that it modifies its input string by inserting NULLs at the last token locations. This is also validated by cppreference.

However, on trying to validate an example from cplusplus.com, I found that in VS2010 in 32 bit Windows 7, strtok is in fact NOT inserting NULLs in the original string. I was able to determine this by modifying the NULL in the argument from the example to str, and the program loops repeatedly, passing “This” as the token, which (as I interpreted this), is not the same behavior as passing the previous pointer, as cppreference claims.

In addition, I thought maybe the const-ness of string literals may have been at fault, so I copied the string

char str2[] ="- This, a sample string.";
char str[50];
strcpy(str,str2);

and ran it again, but the loop repeated. Debugger shows that the input string is not modified.

Can someone explain where I am going wrong here? edit: I think it’s my interpretation of “The behavior is the same as if the previously stored pointer is passed as str.”

Thank you.

EDIT: exact code:

/* strtok example */
#include <iostream>
#include <stdio.h>
#include <string.h>

int main ()
{
  char str2[] ="- This, a sample string.";
  char str[50];
  strcpy(str,str2);
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (str, " ,.-");
    printf("%s\n", str);
  }
  std::cin.ignore();
  return 0;
}

Output from code:

Splitting string "- This, a sample string." into tokens:
This
- This
This
- This
This
- This
This
- This
This
- This
This
- This
This
- This

EDIT: RESOLVED Should I delete this garbage or let it stay? haha I don’t want to take points away from the poor people who had to deal with this

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:49:49+00:00Added an answer on June 14, 2026 at 8:49 am

    Firstly, it is not NULL that is inserted into the original string, it is a zero-character that is inserted. While I understand that this is probably what you wanted to say, it is still not a good idea to involve a well-known and completely unrelated macro NULL.

    Secondly, if strtok fails inserting zero characters into the original string, it simply won’t work as intended. For this reason I strongly believe that you somehow misinterpreted the results of your experiments. strtok does modify the input string even in VS2010 under 32 bit Windows 7.


    The output from your code you posted clearly shows that the string was modified. The original value of str was "- This, a sample string.". The value of str printed from inside the loop is just "- This". The string got truncated specifically because strtok inserted a zero-character right after "- This" (more precisely, the , character got replaced with \0 character).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Specifically, suppose I start with the string string =hello \'i am \' me And
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.