473,398 Members | 2,368 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,398 software developers and data experts.

behaviour of strtok()

I made some tests and noticed that after the function that call strtok() finish, the strings generated by strtok() doesnt exist anymore. Why it hapens and how to work around?
Mar 7 '10 #1

✓ answered by Banfa

strtok doesn't generate any strings, it just mangles and returns pointers into the middle of the string you provide to it on the first call.

Are you allocating temporary memory in your function and freeing it? Because that would explain the behaviour you describe. Alternitively post your code.

6 2267
Banfa
9,065 Expert Mod 8TB
strtok doesn't generate any strings, it just mangles and returns pointers into the middle of the string you provide to it on the first call.

Are you allocating temporary memory in your function and freeing it? Because that would explain the behaviour you describe. Alternitively post your code.
Mar 7 '10 #2
Thanks, I understood this part.

Now I'd like to know why i get a segmentation fault when passing the variable text to function split.

Expand|Select|Wrap|Line Numbers
  1. char** split(char* text) {
  2.  
  3.     char **array = malloc(sizeof(char*)*50);
  4.     int loop;
  5.  
  6.     array[0]= strtok(text," -:!");
  7.  
  8.     if(array[0]==NULL)
  9.     {
  10.       printf("No test to search.\n");
  11.       exit(0);
  12.     }
  13.  
  14.     for(loop=1;loop<50;loop++)
  15.     {
  16.       array[loop]=strtok(NULL," -:!");
  17.       if(array[loop]==NULL)
  18.         break;
  19.    }
  20.  
  21.    return array;
  22. }
  23.  
  24. int main(void) {
  25.  
  26.     char* text = "Rio2010 – Copa do mundo: Brasil!";
  27.     char** array = split(text);
  28.     int loop;
  29.  
  30.     for(loop=0;loop<50;loop++)
  31.     {
  32.       if(array[loop]==NULL)
  33.         break;
  34.       printf("Item #%d is %s.\n",loop,array[loop]);
  35.     }
  36.  
  37.     return 0;
  38. }
When I use malloc, it work properly

Expand|Select|Wrap|Line Numbers
  1. char *texto = malloc(sizeof(char)*33);
  2.   texto[0] = 'R';
  3.   texto[1] = 'i';
  4.   texto[2] = 'o';
  5.   texto[3] = '2';
  6.   texto[4] = '0';
  7.   texto[5] = '1';
  8.   texto[6] = '0';
  9.   texto[7] = ' ';
  10.   texto[8] = '-';
  11.   texto[9] = ' ';
  12.   texto[10] = 'C';
  13.   texto[11] = 'o';
  14.   texto[12] = 'p';
  15.   texto[13] = 'a';
  16.   texto[14] = ' ';
  17.   texto[15] = 'd';
  18.   texto[16] = 'o';
  19.   texto[17] = ' ';
  20.   texto[18] = 'm';
  21.   texto[19] = 'u';
  22.   texto[20] = 'n';
  23.   texto[21] = 'd';
  24.   texto[22] = 'o';
  25.   texto[23] = ':';
  26.   texto[24] = ' ';
  27.   texto[25] = 'B';
  28.   texto[26] = 'r';
  29.   texto[27] = 'a';
  30.   texto[28] = 's';
  31.   texto[29] = 'i';
  32.   texto[30] = 'l';
  33.   texto[31] = '!';
  34.   texto[32] = '\0';
Mar 7 '10 #3
Banfa
9,065 Expert Mod 8TB
That is because test points to a character constant "Rio2010 – Copa do mundo: Brasil!". On a lot of platforms character constants are actually constant so an attempt to modify them is an error and strtok would attempt to modify the string passed to it.

Generally it is best to declare pointers to character constants as

const char* text = "Rio2010 – Copa do mundo: Brasil!";

to void this issue, the const on the pointer will cause a compiler error if passed to a function expecting a non-const pointer.

You don't get the error when you malloc because you has allocated writable data to contain the string. There are other ways to do this

char* text = strdup("Rio2010 – Copa do mundo: Brasil!");

Also allocates the data on the heap (similarly to malloc) but also does the copy for you. The pointer returned from strdup should be free'd. Or you could put the data on the stack like this

char text[] = "Rio2010 – Copa do mundo: Brasil!";

This allocates an array on the stack, the compiler automatically calculates the size for you and initialises the array as well.
Mar 7 '10 #4
Thank you again, Banfa.

One more thing, inside the function split, I'd like to to alocate memory for the array according to the number of substrings. There is a way to do this?
Mar 8 '10 #5
Banfa
9,065 Expert Mod 8TB
Bot easily and if you did you would have to have a way to pass that number back to the caller. You could do something link parse the string twice, the first time counting the number of resulting strings and the second time creating your array. Or you could use realloc to repeatedly increase the size of the array as required but this would be very in efficient if the array ended up being large.

Alternatively you could do something like return a link list rather than an array.
Mar 8 '10 #6
Thank you. I'm gonna use the realloc. I think it's better for my case.
Mar 8 '10 #7

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

Similar topics

13
by: ern | last post by:
I'm using strtok( ) to capture lines of input. After I call "splitCommand", I call strtok( ) again to get the next line. Strtok( ) returns NULL (but there is more in the file...). That didn't...
20
by: bubunia2000 | last post by:
Hi all, I heard that strtok is not thread safe. So I want to write a sample program which will tokenize string without using strtok. Can I get a sample source code for the same. For exp:...
8
by: hu | last post by:
hi, everybody! I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0. Program is simple, but mistake is confusing. First, the below code can get right outcome:"ello world, hello...
4
by: Michael | last post by:
Hi, I have a proble I don't understand when using strtok(). It seems that if I make a call to strtok(), then make a call to another function that also makes use of strtok(), the original call is...
3
by: nomad5000 | last post by:
Hi everybody! I'm having trouble using strtok to fill a matrix with int nrs. from a file. the code that is not working is the following: #include <iostream> #include <fstream> #include...
2
by: =?Utf-8?B?c3Zlbg==?= | last post by:
Hi, what would be the default strtok behaviour when a null string is passed into the string parameter in the first call to strtok? I would expect a null pointer to be returned but it is sometimes...
29
by: Pietro Cerutti | last post by:
Hello, here I have a strange problem with a real simple strtok example. The program is as follows: ### BEGIN STRTOK ### #include <string.h> #include <stdio.h>
11
by: Lothar Behrens | last post by:
Hi, I have selected strtok to be used in my string replacement function. But I lost the last token, if there is one. This string would be replaced select "name", "vorname", "userid",...
11
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
12
by: Pilcrow | last post by:
Here is a quick program, together with its output, that illustrates what I consider to be a deficiency of the standard function strtok from <string.h>: I am using C:\>gcc --version gcc (GCC)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.