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

Why strtok() don't work with char *?

Hi, I'm trying to write a simple program in VC++ 2010 to tokenize a sentence.
Yet, the code I wrote do not cause any compilation errors( regardless of warnings about strtok ) but when its executed it stops working. This is the code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. int main(){
  7.     char *line = "int a = 10, b; ";
  8.     char *token;
  9.  
  10.     token = strtok( line, " " ); 
  11.  
  12.     cout << token;
  13.  
  14.         return 0;
  15. }
This program seems to have a problem with strtok() because of these:




And one another thing: why when I change
Expand|Select|Wrap|Line Numbers
  1.  char *line 
to
Expand|Select|Wrap|Line Numbers
  1.  char line[] 
... it works correctly?
Jan 24 '11 #1

✓ answered by horace1

in line 7
Expand|Select|Wrap|Line Numbers
  1.     char *line = "int a = 10, b; ";
the text "int a = 10, b; " is a string constant and the gcc compiler displays the warning (if warnings are enbled)
Expand|Select|Wrap|Line Numbers
  1.  warning: deprecated conversion from string constant to 'char*'
  2.  
therefore the variable line points to a string constant
you can print its contents OK so
Expand|Select|Wrap|Line Numbers
  1.    cout << line << endl;
  2.  
because the statement does not attempt to modify the string

however, the first parameter to strtok() cannot be a pointer to a constant as the contents of the string are modified and broken into smaller strings (tokens) by strtok(). see
http://www.cplusplus.com/reference/c...string/strtok/
it is when strtok() trys to modify the constant string "int a = 10, b; " the program crashes with a memory error.

changing line 7 to
Expand|Select|Wrap|Line Numbers
  1.     char line[] = "int a = 10, b; ";
  2.  
works because the string not a constant it is an array which can be modified

2 4786
horace1
1,510 Expert 1GB
in line 7
Expand|Select|Wrap|Line Numbers
  1.     char *line = "int a = 10, b; ";
the text "int a = 10, b; " is a string constant and the gcc compiler displays the warning (if warnings are enbled)
Expand|Select|Wrap|Line Numbers
  1.  warning: deprecated conversion from string constant to 'char*'
  2.  
therefore the variable line points to a string constant
you can print its contents OK so
Expand|Select|Wrap|Line Numbers
  1.    cout << line << endl;
  2.  
because the statement does not attempt to modify the string

however, the first parameter to strtok() cannot be a pointer to a constant as the contents of the string are modified and broken into smaller strings (tokens) by strtok(). see
http://www.cplusplus.com/reference/c...string/strtok/
it is when strtok() trys to modify the constant string "int a = 10, b; " the program crashes with a memory error.

changing line 7 to
Expand|Select|Wrap|Line Numbers
  1.     char line[] = "int a = 10, b; ";
  2.  
works because the string not a constant it is an array which can be modified
Jan 24 '11 #2
Banfa
9,065 Expert Mod 8TB
works because the string not a constant it is an array which can be modified
Strictly and pedantically speaking the string is still a constant but it is copied into a non-constant array of char line and this is what is modified.
Jan 24 '11 #3

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

Similar topics

1
by: Dave | last post by:
I am doing a complete backup on a sql 7 db and then doing a complete restore (with overwrite existing db) on a sql 2000 server. This is now our hot standby server. I have the process automated and...
0
by: LR | last post by:
Hi, I just installed visual studio .Net and everything was allright since à try to code in Form1.cs... The keys listed in subject line dont work. So I cannot make a line feed in my code ! What...
0
by: Mate Visky | last post by:
hi, i have a little problame. i got this xml file: ---- <?xml version="1.0" encoding="UTF-8" ?> - <out> - <design> - <html> - <!]> </html>
1
by: Sebastian Sosna | last post by:
Hello NG! Iam trying to write Access Control Settings for Users in Active Dir. First what i do is to delegate a Trustee in a Container, with permissions. This works fine. Lets take Guests as...
4
by: Abbiento Morgan | last post by:
I try to expalin my problem Ive created this classes class DataType { ... public int Count; ... }
5
by: raagz | last post by:
Hi, I am using the .NET validation controls for validating . The validation works fine with IE but none of them work with Netscape 6 browser. Do i have to configure something in vS.NET to...
0
by: m3rajk | last post by:
i tried 'show databases;' and 'show database;' with NO response. i'm pretty sure it's the frist one that lists the databases on postgres sql and myssql i have used those two but work is using...
7
by: Maarten | last post by:
Hi, I have a gridview (2.0), in the page load I remove a couple of columns from the gridview which I dont need. (All the columns are databound) This works fine. But after a postback all the...
2
by: tasiekk | last post by:
I have 1 lil problem. I make simple Win Forms app and i put there a tab control. There i make two pages. In each page i make 1 button. In tabpage1 button1 and in tabpage2 button2. And i make access...
3
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Vista sp1 C# Hi: after I opened a vs2005 IDE... my breakpoints when debugging in vs2008 dont work any more, any ideas? the test finished but It does not stop. Thanks
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.