strTok function (thread safe, supports empty tokens, doesn't change string)String case reverse function in CGet line from string functionTDD: String Calculator KataC - K&R getint() variationSimple function to generate an HTML-safe stringGeneric Pairing Heap PerformancePattern for writing a generic string transformation functionChange a string into a function/def activatorC++ string tokenizing without streams, with certain conditionsRead consecutive blanks in array

Do I have a twin with permutated remainders?

How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?

Fully-Firstable Anagram Sets

Do infinite dimensional systems make sense?

Watching something be written to a file live with tail

What's the output of a record needle playing an out-of-speed record

How to efficiently unroll a matrix by value with numpy?

What does the "remote control" for a QF-4 look like?

What does it mean to describe someone as a butt steak?

Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?

Malcev's paper "On a class of homogeneous spaces" in English

How to determine what difficulty is right for the game?

Has there ever been an airliner design involving reducing generator load by installing solar panels?

Intersection point of 2 lines defined by 2 points each

dbcc cleantable batch size explanation

Why doesn't H₄O²⁺ exist?

How to format long polynomial?

Character reincarnated...as a snail

Is it tax fraud for an individual to declare non-taxable revenue as taxable income? (US tax laws)

Alternative to sending password over mail?

What is a clear way to write a bar that has an extra beat?

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

Why is consensus so controversial in Britain?

Can I ask the recruiters in my resume to put the reason why I am rejected?



strTok function (thread safe, supports empty tokens, doesn't change string)


String case reverse function in CGet line from string functionTDD: String Calculator KataC - K&R getint() variationSimple function to generate an HTML-safe stringGeneric Pairing Heap PerformancePattern for writing a generic string transformation functionChange a string into a function/def activatorC++ string tokenizing without streams, with certain conditionsRead consecutive blanks in array






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3












$begingroup$


I'm new to C language and want to explode a string like we do in PHP explode() function, I searched for a built-in function with the C standard library, and I found strtok , but It doesn't support empty tokens like 1,2,3,,5 . Inspired by the answers I found in this SO question I made this function, it is supposed to be thread safe and support empty tokens and doesn't change the original string



char* strTok(char** newString, char* delimiter)

char* string = *newString;
char* delimiterFound = (char*) 0;
int tokLenght = 0;
char* tok = (char*) 0;

if(!string) return (char*) 0;

delimiterFound = strstr(string, delimiter);

if(delimiterFound)
tokLenght = delimiterFound-string;
else
tokLenght = strlen(string);


tok = malloc(tokLenght + 1);
memcpy(tok, string, tokLenght);
tok[tokLenght] = '';

*newString = delimiterFound ? delimiterFound + strlen(delimiter) : (char*)0;

return tok;



I designed it to be used like



char* input = "1,2,3,4,5,6,7,,,10,";
char** inputP = &input;
char* tok;
while( (tok=strTok(inputP, ",")) )
printf("%sn", tok);










share|improve this question









$endgroup$







  • 1




    $begingroup$
    Better user-interface then the original strtok. You may be interested in strsep, too. code.woboq.org/userspace/glibc/string/strsep.c.html
    $endgroup$
    – Neil Edelman
    4 hours ago










  • $begingroup$
    @NeilEdelman thanks I never saw this function before, I will check it.
    $endgroup$
    – Accountant م
    4 hours ago






  • 1




    $begingroup$
    It's not in the standard C libraries, but in POSIX, (any type of gcc.) However, like strtok, it obliterates the char to replace it with , so it's not the same.
    $endgroup$
    – Neil Edelman
    4 hours ago

















3












$begingroup$


I'm new to C language and want to explode a string like we do in PHP explode() function, I searched for a built-in function with the C standard library, and I found strtok , but It doesn't support empty tokens like 1,2,3,,5 . Inspired by the answers I found in this SO question I made this function, it is supposed to be thread safe and support empty tokens and doesn't change the original string



char* strTok(char** newString, char* delimiter)

char* string = *newString;
char* delimiterFound = (char*) 0;
int tokLenght = 0;
char* tok = (char*) 0;

if(!string) return (char*) 0;

delimiterFound = strstr(string, delimiter);

if(delimiterFound)
tokLenght = delimiterFound-string;
else
tokLenght = strlen(string);


tok = malloc(tokLenght + 1);
memcpy(tok, string, tokLenght);
tok[tokLenght] = '';

*newString = delimiterFound ? delimiterFound + strlen(delimiter) : (char*)0;

return tok;



I designed it to be used like



char* input = "1,2,3,4,5,6,7,,,10,";
char** inputP = &input;
char* tok;
while( (tok=strTok(inputP, ",")) )
printf("%sn", tok);










share|improve this question









$endgroup$







  • 1




    $begingroup$
    Better user-interface then the original strtok. You may be interested in strsep, too. code.woboq.org/userspace/glibc/string/strsep.c.html
    $endgroup$
    – Neil Edelman
    4 hours ago










  • $begingroup$
    @NeilEdelman thanks I never saw this function before, I will check it.
    $endgroup$
    – Accountant م
    4 hours ago






  • 1




    $begingroup$
    It's not in the standard C libraries, but in POSIX, (any type of gcc.) However, like strtok, it obliterates the char to replace it with , so it's not the same.
    $endgroup$
    – Neil Edelman
    4 hours ago













3












3








3





$begingroup$


I'm new to C language and want to explode a string like we do in PHP explode() function, I searched for a built-in function with the C standard library, and I found strtok , but It doesn't support empty tokens like 1,2,3,,5 . Inspired by the answers I found in this SO question I made this function, it is supposed to be thread safe and support empty tokens and doesn't change the original string



char* strTok(char** newString, char* delimiter)

char* string = *newString;
char* delimiterFound = (char*) 0;
int tokLenght = 0;
char* tok = (char*) 0;

if(!string) return (char*) 0;

delimiterFound = strstr(string, delimiter);

if(delimiterFound)
tokLenght = delimiterFound-string;
else
tokLenght = strlen(string);


tok = malloc(tokLenght + 1);
memcpy(tok, string, tokLenght);
tok[tokLenght] = '';

*newString = delimiterFound ? delimiterFound + strlen(delimiter) : (char*)0;

return tok;



I designed it to be used like



char* input = "1,2,3,4,5,6,7,,,10,";
char** inputP = &input;
char* tok;
while( (tok=strTok(inputP, ",")) )
printf("%sn", tok);










share|improve this question









$endgroup$




I'm new to C language and want to explode a string like we do in PHP explode() function, I searched for a built-in function with the C standard library, and I found strtok , but It doesn't support empty tokens like 1,2,3,,5 . Inspired by the answers I found in this SO question I made this function, it is supposed to be thread safe and support empty tokens and doesn't change the original string



char* strTok(char** newString, char* delimiter)

char* string = *newString;
char* delimiterFound = (char*) 0;
int tokLenght = 0;
char* tok = (char*) 0;

if(!string) return (char*) 0;

delimiterFound = strstr(string, delimiter);

if(delimiterFound)
tokLenght = delimiterFound-string;
else
tokLenght = strlen(string);


tok = malloc(tokLenght + 1);
memcpy(tok, string, tokLenght);
tok[tokLenght] = '';

*newString = delimiterFound ? delimiterFound + strlen(delimiter) : (char*)0;

return tok;



I designed it to be used like



char* input = "1,2,3,4,5,6,7,,,10,";
char** inputP = &input;
char* tok;
while( (tok=strTok(inputP, ",")) )
printf("%sn", tok);







beginner c strings






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 6 hours ago









Accountant مAccountant م

1777




1777







  • 1




    $begingroup$
    Better user-interface then the original strtok. You may be interested in strsep, too. code.woboq.org/userspace/glibc/string/strsep.c.html
    $endgroup$
    – Neil Edelman
    4 hours ago










  • $begingroup$
    @NeilEdelman thanks I never saw this function before, I will check it.
    $endgroup$
    – Accountant م
    4 hours ago






  • 1




    $begingroup$
    It's not in the standard C libraries, but in POSIX, (any type of gcc.) However, like strtok, it obliterates the char to replace it with , so it's not the same.
    $endgroup$
    – Neil Edelman
    4 hours ago












  • 1




    $begingroup$
    Better user-interface then the original strtok. You may be interested in strsep, too. code.woboq.org/userspace/glibc/string/strsep.c.html
    $endgroup$
    – Neil Edelman
    4 hours ago










  • $begingroup$
    @NeilEdelman thanks I never saw this function before, I will check it.
    $endgroup$
    – Accountant م
    4 hours ago






  • 1




    $begingroup$
    It's not in the standard C libraries, but in POSIX, (any type of gcc.) However, like strtok, it obliterates the char to replace it with , so it's not the same.
    $endgroup$
    – Neil Edelman
    4 hours ago







1




1




$begingroup$
Better user-interface then the original strtok. You may be interested in strsep, too. code.woboq.org/userspace/glibc/string/strsep.c.html
$endgroup$
– Neil Edelman
4 hours ago




$begingroup$
Better user-interface then the original strtok. You may be interested in strsep, too. code.woboq.org/userspace/glibc/string/strsep.c.html
$endgroup$
– Neil Edelman
4 hours ago












$begingroup$
@NeilEdelman thanks I never saw this function before, I will check it.
$endgroup$
– Accountant م
4 hours ago




$begingroup$
@NeilEdelman thanks I never saw this function before, I will check it.
$endgroup$
– Accountant م
4 hours ago




1




1




$begingroup$
It's not in the standard C libraries, but in POSIX, (any type of gcc.) However, like strtok, it obliterates the char to replace it with , so it's not the same.
$endgroup$
– Neil Edelman
4 hours ago




$begingroup$
It's not in the standard C libraries, but in POSIX, (any type of gcc.) However, like strtok, it obliterates the char to replace it with , so it's not the same.
$endgroup$
– Neil Edelman
4 hours ago










2 Answers
2






active

oldest

votes


















2












$begingroup$

  • delimiterFound + strlen(delimiter) sounds like a bug. If the delimiter is longer than one character, *newString will point too far into the original, maybe even beyond the end. Correct me if I am wrong, delimiterFound + 1 is what you are actually after.



  • Modern C allows, and strongly encourages, to declare variables as close to their use a possible. Consider



    char * delimiterFound = strstr(string, delimiter);
    ....
    char * tok = malloc(tokLenght + 1);


    etc.



  • Always test that malloc didn't fail.



  • More spaces - around keywords, braces, etc - definitely improve readability:



     if (....) 
    ....
    else
    ....







share|improve this answer









$endgroup$








  • 1




    $begingroup$
    Thaaank you very much for these precious points, regarding the delimiter length bug, ummmm, I want to support long delimiters more than 1 characters like the boundary string in http requests that has content-type multi-part, and I don't think it's a bug because delimiterFound + strlen(delimiter) can never be after the 0 byte that terminates the original string!, right ? "fooDELIMITER" 3 + 12
    $endgroup$
    – Accountant م
    4 hours ago










  • $begingroup$
    @Accountantم Long delimiters here refer to, say ",;.", in where any character delimits the string on its own right.
    $endgroup$
    – vnp
    4 hours ago










  • $begingroup$
    I didn't get it, I'm sorry, can you please give me an example input that can break this code, exploiting this bug ?
    $endgroup$
    – Accountant م
    4 hours ago






  • 1




    $begingroup$
    @Accountantم Sorry for not being clear. I should realize that your intentions are different (and read man strstr more carefully). Consider it my blinder - since you mentioned strtok, I expected the strtok semantics.
    $endgroup$
    – vnp
    4 hours ago










  • $begingroup$
    It's my fault because I said it's strtok, I wanted strtok that can support delimiters more than 1 characters, because I need this feature a lot. So do you mean it's not a bug ??
    $endgroup$
    – Accountant م
    4 hours ago


















2












$begingroup$

From a readability viewpoint, you should use NULL instead of (char*) 0 as it is easier to recognize what you're trying to do. Also, the tokLenght misspells "length", and should probably be tokLength.



You leak memory, as the memory allocated to hold the returned string is never freed.






share|improve this answer









$endgroup$












  • $begingroup$
    Thank you very much I will use NULL from now on, and I will remember to free() memory , 'I miss PHP garbage collector :(', I didn't get the tokLength spelling note, aren't they the same ?
    $endgroup$
    – Accountant م
    4 hours ago







  • 1




    $begingroup$
    It's a matter of style, and including .h if you want to use NULL. However, you don't have to cast (char *)0, just use 0 (or NULL.) It knows from the return type.
    $endgroup$
    – Neil Edelman
    4 hours ago











Your Answer





StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216956%2fstrtok-function-thread-safe-supports-empty-tokens-doesnt-change-string%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2












$begingroup$

  • delimiterFound + strlen(delimiter) sounds like a bug. If the delimiter is longer than one character, *newString will point too far into the original, maybe even beyond the end. Correct me if I am wrong, delimiterFound + 1 is what you are actually after.



  • Modern C allows, and strongly encourages, to declare variables as close to their use a possible. Consider



    char * delimiterFound = strstr(string, delimiter);
    ....
    char * tok = malloc(tokLenght + 1);


    etc.



  • Always test that malloc didn't fail.



  • More spaces - around keywords, braces, etc - definitely improve readability:



     if (....) 
    ....
    else
    ....







share|improve this answer









$endgroup$








  • 1




    $begingroup$
    Thaaank you very much for these precious points, regarding the delimiter length bug, ummmm, I want to support long delimiters more than 1 characters like the boundary string in http requests that has content-type multi-part, and I don't think it's a bug because delimiterFound + strlen(delimiter) can never be after the 0 byte that terminates the original string!, right ? "fooDELIMITER" 3 + 12
    $endgroup$
    – Accountant م
    4 hours ago










  • $begingroup$
    @Accountantم Long delimiters here refer to, say ",;.", in where any character delimits the string on its own right.
    $endgroup$
    – vnp
    4 hours ago










  • $begingroup$
    I didn't get it, I'm sorry, can you please give me an example input that can break this code, exploiting this bug ?
    $endgroup$
    – Accountant م
    4 hours ago






  • 1




    $begingroup$
    @Accountantم Sorry for not being clear. I should realize that your intentions are different (and read man strstr more carefully). Consider it my blinder - since you mentioned strtok, I expected the strtok semantics.
    $endgroup$
    – vnp
    4 hours ago










  • $begingroup$
    It's my fault because I said it's strtok, I wanted strtok that can support delimiters more than 1 characters, because I need this feature a lot. So do you mean it's not a bug ??
    $endgroup$
    – Accountant م
    4 hours ago















2












$begingroup$

  • delimiterFound + strlen(delimiter) sounds like a bug. If the delimiter is longer than one character, *newString will point too far into the original, maybe even beyond the end. Correct me if I am wrong, delimiterFound + 1 is what you are actually after.



  • Modern C allows, and strongly encourages, to declare variables as close to their use a possible. Consider



    char * delimiterFound = strstr(string, delimiter);
    ....
    char * tok = malloc(tokLenght + 1);


    etc.



  • Always test that malloc didn't fail.



  • More spaces - around keywords, braces, etc - definitely improve readability:



     if (....) 
    ....
    else
    ....







share|improve this answer









$endgroup$








  • 1




    $begingroup$
    Thaaank you very much for these precious points, regarding the delimiter length bug, ummmm, I want to support long delimiters more than 1 characters like the boundary string in http requests that has content-type multi-part, and I don't think it's a bug because delimiterFound + strlen(delimiter) can never be after the 0 byte that terminates the original string!, right ? "fooDELIMITER" 3 + 12
    $endgroup$
    – Accountant م
    4 hours ago










  • $begingroup$
    @Accountantم Long delimiters here refer to, say ",;.", in where any character delimits the string on its own right.
    $endgroup$
    – vnp
    4 hours ago










  • $begingroup$
    I didn't get it, I'm sorry, can you please give me an example input that can break this code, exploiting this bug ?
    $endgroup$
    – Accountant م
    4 hours ago






  • 1




    $begingroup$
    @Accountantم Sorry for not being clear. I should realize that your intentions are different (and read man strstr more carefully). Consider it my blinder - since you mentioned strtok, I expected the strtok semantics.
    $endgroup$
    – vnp
    4 hours ago










  • $begingroup$
    It's my fault because I said it's strtok, I wanted strtok that can support delimiters more than 1 characters, because I need this feature a lot. So do you mean it's not a bug ??
    $endgroup$
    – Accountant م
    4 hours ago













2












2








2





$begingroup$

  • delimiterFound + strlen(delimiter) sounds like a bug. If the delimiter is longer than one character, *newString will point too far into the original, maybe even beyond the end. Correct me if I am wrong, delimiterFound + 1 is what you are actually after.



  • Modern C allows, and strongly encourages, to declare variables as close to their use a possible. Consider



    char * delimiterFound = strstr(string, delimiter);
    ....
    char * tok = malloc(tokLenght + 1);


    etc.



  • Always test that malloc didn't fail.



  • More spaces - around keywords, braces, etc - definitely improve readability:



     if (....) 
    ....
    else
    ....







share|improve this answer









$endgroup$



  • delimiterFound + strlen(delimiter) sounds like a bug. If the delimiter is longer than one character, *newString will point too far into the original, maybe even beyond the end. Correct me if I am wrong, delimiterFound + 1 is what you are actually after.



  • Modern C allows, and strongly encourages, to declare variables as close to their use a possible. Consider



    char * delimiterFound = strstr(string, delimiter);
    ....
    char * tok = malloc(tokLenght + 1);


    etc.



  • Always test that malloc didn't fail.



  • More spaces - around keywords, braces, etc - definitely improve readability:



     if (....) 
    ....
    else
    ....








share|improve this answer












share|improve this answer



share|improve this answer










answered 5 hours ago









vnpvnp

40.6k233103




40.6k233103







  • 1




    $begingroup$
    Thaaank you very much for these precious points, regarding the delimiter length bug, ummmm, I want to support long delimiters more than 1 characters like the boundary string in http requests that has content-type multi-part, and I don't think it's a bug because delimiterFound + strlen(delimiter) can never be after the 0 byte that terminates the original string!, right ? "fooDELIMITER" 3 + 12
    $endgroup$
    – Accountant م
    4 hours ago










  • $begingroup$
    @Accountantم Long delimiters here refer to, say ",;.", in where any character delimits the string on its own right.
    $endgroup$
    – vnp
    4 hours ago










  • $begingroup$
    I didn't get it, I'm sorry, can you please give me an example input that can break this code, exploiting this bug ?
    $endgroup$
    – Accountant م
    4 hours ago






  • 1




    $begingroup$
    @Accountantم Sorry for not being clear. I should realize that your intentions are different (and read man strstr more carefully). Consider it my blinder - since you mentioned strtok, I expected the strtok semantics.
    $endgroup$
    – vnp
    4 hours ago










  • $begingroup$
    It's my fault because I said it's strtok, I wanted strtok that can support delimiters more than 1 characters, because I need this feature a lot. So do you mean it's not a bug ??
    $endgroup$
    – Accountant م
    4 hours ago












  • 1




    $begingroup$
    Thaaank you very much for these precious points, regarding the delimiter length bug, ummmm, I want to support long delimiters more than 1 characters like the boundary string in http requests that has content-type multi-part, and I don't think it's a bug because delimiterFound + strlen(delimiter) can never be after the 0 byte that terminates the original string!, right ? "fooDELIMITER" 3 + 12
    $endgroup$
    – Accountant م
    4 hours ago










  • $begingroup$
    @Accountantم Long delimiters here refer to, say ",;.", in where any character delimits the string on its own right.
    $endgroup$
    – vnp
    4 hours ago










  • $begingroup$
    I didn't get it, I'm sorry, can you please give me an example input that can break this code, exploiting this bug ?
    $endgroup$
    – Accountant م
    4 hours ago






  • 1




    $begingroup$
    @Accountantم Sorry for not being clear. I should realize that your intentions are different (and read man strstr more carefully). Consider it my blinder - since you mentioned strtok, I expected the strtok semantics.
    $endgroup$
    – vnp
    4 hours ago










  • $begingroup$
    It's my fault because I said it's strtok, I wanted strtok that can support delimiters more than 1 characters, because I need this feature a lot. So do you mean it's not a bug ??
    $endgroup$
    – Accountant م
    4 hours ago







1




1




$begingroup$
Thaaank you very much for these precious points, regarding the delimiter length bug, ummmm, I want to support long delimiters more than 1 characters like the boundary string in http requests that has content-type multi-part, and I don't think it's a bug because delimiterFound + strlen(delimiter) can never be after the 0 byte that terminates the original string!, right ? "fooDELIMITER" 3 + 12
$endgroup$
– Accountant م
4 hours ago




$begingroup$
Thaaank you very much for these precious points, regarding the delimiter length bug, ummmm, I want to support long delimiters more than 1 characters like the boundary string in http requests that has content-type multi-part, and I don't think it's a bug because delimiterFound + strlen(delimiter) can never be after the 0 byte that terminates the original string!, right ? "fooDELIMITER" 3 + 12
$endgroup$
– Accountant م
4 hours ago












$begingroup$
@Accountantم Long delimiters here refer to, say ",;.", in where any character delimits the string on its own right.
$endgroup$
– vnp
4 hours ago




$begingroup$
@Accountantم Long delimiters here refer to, say ",;.", in where any character delimits the string on its own right.
$endgroup$
– vnp
4 hours ago












$begingroup$
I didn't get it, I'm sorry, can you please give me an example input that can break this code, exploiting this bug ?
$endgroup$
– Accountant م
4 hours ago




$begingroup$
I didn't get it, I'm sorry, can you please give me an example input that can break this code, exploiting this bug ?
$endgroup$
– Accountant م
4 hours ago




1




1




$begingroup$
@Accountantم Sorry for not being clear. I should realize that your intentions are different (and read man strstr more carefully). Consider it my blinder - since you mentioned strtok, I expected the strtok semantics.
$endgroup$
– vnp
4 hours ago




$begingroup$
@Accountantم Sorry for not being clear. I should realize that your intentions are different (and read man strstr more carefully). Consider it my blinder - since you mentioned strtok, I expected the strtok semantics.
$endgroup$
– vnp
4 hours ago












$begingroup$
It's my fault because I said it's strtok, I wanted strtok that can support delimiters more than 1 characters, because I need this feature a lot. So do you mean it's not a bug ??
$endgroup$
– Accountant م
4 hours ago




$begingroup$
It's my fault because I said it's strtok, I wanted strtok that can support delimiters more than 1 characters, because I need this feature a lot. So do you mean it's not a bug ??
$endgroup$
– Accountant م
4 hours ago













2












$begingroup$

From a readability viewpoint, you should use NULL instead of (char*) 0 as it is easier to recognize what you're trying to do. Also, the tokLenght misspells "length", and should probably be tokLength.



You leak memory, as the memory allocated to hold the returned string is never freed.






share|improve this answer









$endgroup$












  • $begingroup$
    Thank you very much I will use NULL from now on, and I will remember to free() memory , 'I miss PHP garbage collector :(', I didn't get the tokLength spelling note, aren't they the same ?
    $endgroup$
    – Accountant م
    4 hours ago







  • 1




    $begingroup$
    It's a matter of style, and including .h if you want to use NULL. However, you don't have to cast (char *)0, just use 0 (or NULL.) It knows from the return type.
    $endgroup$
    – Neil Edelman
    4 hours ago















2












$begingroup$

From a readability viewpoint, you should use NULL instead of (char*) 0 as it is easier to recognize what you're trying to do. Also, the tokLenght misspells "length", and should probably be tokLength.



You leak memory, as the memory allocated to hold the returned string is never freed.






share|improve this answer









$endgroup$












  • $begingroup$
    Thank you very much I will use NULL from now on, and I will remember to free() memory , 'I miss PHP garbage collector :(', I didn't get the tokLength spelling note, aren't they the same ?
    $endgroup$
    – Accountant م
    4 hours ago







  • 1




    $begingroup$
    It's a matter of style, and including .h if you want to use NULL. However, you don't have to cast (char *)0, just use 0 (or NULL.) It knows from the return type.
    $endgroup$
    – Neil Edelman
    4 hours ago













2












2








2





$begingroup$

From a readability viewpoint, you should use NULL instead of (char*) 0 as it is easier to recognize what you're trying to do. Also, the tokLenght misspells "length", and should probably be tokLength.



You leak memory, as the memory allocated to hold the returned string is never freed.






share|improve this answer









$endgroup$



From a readability viewpoint, you should use NULL instead of (char*) 0 as it is easier to recognize what you're trying to do. Also, the tokLenght misspells "length", and should probably be tokLength.



You leak memory, as the memory allocated to hold the returned string is never freed.







share|improve this answer












share|improve this answer



share|improve this answer










answered 5 hours ago









1201ProgramAlarm1201ProgramAlarm

3,6632925




3,6632925











  • $begingroup$
    Thank you very much I will use NULL from now on, and I will remember to free() memory , 'I miss PHP garbage collector :(', I didn't get the tokLength spelling note, aren't they the same ?
    $endgroup$
    – Accountant م
    4 hours ago







  • 1




    $begingroup$
    It's a matter of style, and including .h if you want to use NULL. However, you don't have to cast (char *)0, just use 0 (or NULL.) It knows from the return type.
    $endgroup$
    – Neil Edelman
    4 hours ago
















  • $begingroup$
    Thank you very much I will use NULL from now on, and I will remember to free() memory , 'I miss PHP garbage collector :(', I didn't get the tokLength spelling note, aren't they the same ?
    $endgroup$
    – Accountant م
    4 hours ago







  • 1




    $begingroup$
    It's a matter of style, and including .h if you want to use NULL. However, you don't have to cast (char *)0, just use 0 (or NULL.) It knows from the return type.
    $endgroup$
    – Neil Edelman
    4 hours ago















$begingroup$
Thank you very much I will use NULL from now on, and I will remember to free() memory , 'I miss PHP garbage collector :(', I didn't get the tokLength spelling note, aren't they the same ?
$endgroup$
– Accountant م
4 hours ago





$begingroup$
Thank you very much I will use NULL from now on, and I will remember to free() memory , 'I miss PHP garbage collector :(', I didn't get the tokLength spelling note, aren't they the same ?
$endgroup$
– Accountant م
4 hours ago





1




1




$begingroup$
It's a matter of style, and including .h if you want to use NULL. However, you don't have to cast (char *)0, just use 0 (or NULL.) It knows from the return type.
$endgroup$
– Neil Edelman
4 hours ago




$begingroup$
It's a matter of style, and including .h if you want to use NULL. However, you don't have to cast (char *)0, just use 0 (or NULL.) It knows from the return type.
$endgroup$
– Neil Edelman
4 hours ago

















draft saved

draft discarded
















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216956%2fstrtok-function-thread-safe-supports-empty-tokens-doesnt-change-string%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

Tom Holland Mục lục Đầu đời và giáo dục | Sự nghiệp | Cuộc sống cá nhân | Phim tham gia | Giải thưởng và đề cử | Chú thích | Liên kết ngoài | Trình đơn chuyển hướngProfile“Person Details for Thomas Stanley Holland, "England and Wales Birth Registration Index, 1837-2008" — FamilySearch.org”"Meet Tom Holland... the 16-year-old star of The Impossible""Schoolboy actor Tom Holland finds himself in Oscar contention for role in tsunami drama"“Naomi Watts on the Prince William and Harry's reaction to her film about the late Princess Diana”lưu trữ"Holland and Pflueger Are West End's Two New 'Billy Elliots'""I'm so envious of my son, the movie star! British writer Dominic Holland's spent 20 years trying to crack Hollywood - but he's been beaten to it by a very unlikely rival"“Richard and Margaret Povey of Jersey, Channel Islands, UK: Information about Thomas Stanley Holland”"Tom Holland to play Billy Elliot""New Billy Elliot leaving the garage"Billy Elliot the Musical - Tom Holland - Billy"A Tale of four Billys: Tom Holland""The Feel Good Factor""Thames Christian College schoolboys join Myleene Klass for The Feelgood Factor""Government launches £600,000 arts bursaries pilot""BILLY's Chapman, Holland, Gardner & Jackson-Keen Visit Prime Minister""Elton John 'blown away' by Billy Elliot fifth birthday" (video with John's interview and fragments of Holland's performance)"First News interviews Arrietty's Tom Holland"“33rd Critics' Circle Film Awards winners”“National Board of Review Current Awards”Bản gốc"Ron Howard Whaling Tale 'In The Heart Of The Sea' Casts Tom Holland"“'Spider-Man' Finds Tom Holland to Star as New Web-Slinger”lưu trữ“Captain America: Civil War (2016)”“Film Review: ‘Captain America: Civil War’”lưu trữ“‘Captain America: Civil War’ review: Choose your own avenger”lưu trữ“The Lost City of Z reviews”“Sony Pictures and Marvel Studios Find Their 'Spider-Man' Star and Director”“‘Mary Magdalene’, ‘Current War’ & ‘Wind River’ Get 2017 Release Dates From Weinstein”“Lionsgate Unleashing Daisy Ridley & Tom Holland Starrer ‘Chaos Walking’ In Cannes”“PTA's 'Master' Leads Chicago Film Critics Nominations, UPDATED: Houston and Indiana Critics Nominations”“Nominaciones Goya 2013 Telecinco Cinema – ENG”“Jameson Empire Film Awards: Martin Freeman wins best actor for performance in The Hobbit”“34th Annual Young Artist Awards”Bản gốc“Teen Choice Awards 2016—Captain America: Civil War Leads Second Wave of Nominations”“BAFTA Film Award Nominations: ‘La La Land’ Leads Race”“Saturn Awards Nominations 2017: 'Rogue One,' 'Walking Dead' Lead”Tom HollandTom HollandTom HollandTom Hollandmedia.gettyimages.comWorldCat Identities300279794no20130442900000 0004 0355 42791085670554170004732cb16706349t(data)XX5557367