473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

NULL in second call of strtok()

I hve to use NULL as the first arguement in strtok() after first call, i want to know that why i have to use NULL as arguement in place of actual string?
Sep 19 '07 #1
6 19105
sicarie
4,677 Expert Mod 4TB
I hve to use NULL as the first arguement in strtok() after first call, i want to know that why i have to use NULL as arguement in place of actual string?
Well, if you look at the strtok() documentation the first argument is the string you want to be parsed. If you are not passing it a string, you can't parse null for anything but null, and why are you even using strtok() to do nothing?
Sep 19 '07 #2
Studlyami
464 Expert 256MB
The first call you use a char array which has the elements you want parsed.
The second time you call it you pass it NULL as the first parameter to tell function to resume from the last spot in the string. Once the first call is made your char array receives the parsed string. If you don't put NULL you would lose your place and effectivly the last part of your string.
char * c_Ptr = NULL; //temp hold indivisual sections
char c_Ptr1[1000] = {NULL};
fgets(c_Ptr1, 1000, f_ptr) //Grabs a line from an open file

strtok(c_Ptr1, ","); //first one starts at the beginning

c_Ptr = strtok(NULL, ",");
Sep 19 '07 #3
sicarie
4,677 Expert Mod 4TB
The first call you use a char array which has the elements you want parsed.
The second time you call it you pass it NULL as the first parameter to tell function to resume from the last spot in the string. Once the first call is made your char array receives the parsed string. If you don't put NULL you would lose your place and effectivly the last part of your string.
char * c_Ptr = NULL; //temp hold indivisual sections
char c_Ptr1[1000] = {NULL};
fgets(c_Ptr1, 1000, f_ptr) //Grabs a line from an open file

strtok(c_Ptr1, ","); //first one starts at the beginning

c_Ptr = strtok(NULL, ",");
Ah, after the first call. Good catch, Studlyami.
Sep 19 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
Yes, good ol'strtok(). It keeps its position in a static variable. So if you use this thing in a multithreaded program you are on the road to a crash.

strtok() is OK for trivial programs or single-threaded programs. However, for real modern softweare programs, write a parser that doesn't use static variables.
Sep 23 '07 #5
Studlyami
464 Expert 256MB
What type of functions parse without using static variables? I've always used strtok to parse a file.
Sep 24 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
What type of functions parse without using static variables? I've always used strtok to parse a file.
You use a design pattern called State. There is an article on this in the C/C++ Articles forum. The examples are C++ but anything written in C++ can be written in C. It just takes a ton more work.
Sep 25 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: faktujaa | last post by:
Hi All, I have used the available code snippet from microsoft for socket communication. But the following code throws an error mentioned above. Please check the code and help me asap. private...
0
by: Jörg Striegnitz | last post by:
We apologize if you have received this CfP multiple times. -------------------------------------------------------------------- SECOND CALL FOR CONTRIBUTIONS AND PARTICIPATION Workshop on...
3
by: jasonkester | last post by:
Just a heads up for anybody that comes across this in the future. Noticed a strange behavior in RegExp.test() today. Check out the following code. It will alternately display "chokes" and null,...
1
by: --CELKO-- | last post by:
I am working on the third edition of SQL FOR SMARTIES. If anyone has an SQL programming technique, trick or tip that they think should be in the book, drop me an email. You get a virtual beer...
2
by: Eliyahu Goldin | last post by:
I am looking for an ActiveX control for viewing document images. I need only basic viewing functions like zooming, rotating, scaling. Don't need any editing, PhotoShop-style effects, printing,...
6
by: Paul | last post by:
Hello, The following code generates an exception error the *second* time it is called. Can anyone see what I'm doing wrong? There is a similar example in Wrox Professional VB.NET Page 289. ...
0
by: Torsten Grust | last post by:
Second Call for Papers X I M E - P 2 0 0 6 3rd International Workshop on XQuery Implementation, Experience and Perspectives June 30, 2006 Collocated with ACM SIGMOD/PODS 2006, Chicago, USA...
1
by: es_ | last post by:
Hi, On other javascript group I didn't get answer. Maybe You'll know... There's a strange problem with variables. It feels that they work only after second call. It looks like this: jsa="1"...
0
by: Fortis Florin | last post by:
2nd CFP: Workshop on Workflow and Process Management (WfPM'08), September 2008, Timisoara, Romania EXTENDED DEADLINES...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.