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

strtok and char pointers

Hi,

I have a very basic question. What is the difference between

char *string = "This is a string";

and

char string[] = "This is a string";

I'm asking as I've just spent a few hours trying to figure out why I
couldn't get strtok to work - it works on the second declaration of
string, but not on the first.

Thanks,

Tyler
Aug 21 '07 #1
3 4640
In article <sl************************@blackbart.mynetwork> ,
Tyler Smith <ty*********@mail.mcgill.cawrote:
>I have a very basic question. What is the difference between

char *string = "This is a string";

and

char string[] = "This is a string";
The first declares "string" as a pointer; it's initialised to point to
(the first character of) the string "This is a string". If that same
string is used elsewhere in the program, the compiler is allowed to
just keep one copy, and that wouldn't work if the string was
modifiable, so compilers are allowed to keep the string in read-only
memory. You can change the variable to point at some other string,
but you can't change the characters in the initial string.

The second declares "string" as an array of characters - not a
pointer, but as an array itself. The array is initialised to contains
the string "This is a string", and this is (so to speak) the
variable's own memory and you can modify it.

strtok() modifies the string you pass to it, so you can't use a
read-only string such as the one in your first case.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Aug 21 '07 #2
Tyler Smith wrote:
Hi,

I have a very basic question. What is the difference between

char *string = "This is a string";

and

char string[] = "This is a string";

I'm asking as I've just spent a few hours trying to figure out why I
couldn't get strtok to work - it works on the second declaration of
string, but not on the first.
http://c-faq.com questions 8.4 and 8.5 ...
Aug 21 '07 #3
On 2007-08-21, Richard Tobin <ri*****@cogsci.ed.ac.ukwrote:
In article <sl************************@blackbart.mynetwork> ,
Tyler Smith <ty*********@mail.mcgill.cawrote:
>>I have a very basic question. What is the difference between

char *string = "This is a string";

and

char string[] = "This is a string";

The first declares "string" as a pointer; it's initialised to point to
(the first character of) the string "This is a string".

The second declares "string" as an array of characters - not a
pointer, but as an array itself.

strtok() modifies the string you pass to it, so you can't use a
read-only string such as the one in your first case.

My apologies for the delay, my first reply disappeared into the ether.
That said, Thanks for the very helpful explanation, and also to the
other poster who pointed me to the FAQ, which I too frequently
overlook.

Tyler
Aug 23 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ram Laxman | last post by:
Hi all, I have written the following code: /* strtok example */ #include <stdio.h> #include <string.h> static const char * const resultFileName = "param.txt";
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...
8
by: manochavishal | last post by:
Hi I am writing a Program in which i get input as #C1012,S,A#C1013,S,U I want to get C1012,S,A using strtok and then pass this to function CreateVideo which will further strtok this...
33
by: Geometer | last post by:
Hello, and good whatever daytime is at your place.. please can somebody tell me, what the standard behavior of strtok shall be, if it encounters two or more consecutive delimiters like in...
17
by: bofh1234 | last post by:
I need to delimit a string. The delimiters are a semicolon and comma. When I run the program I get a segmentation fault on the first strtok. I followed the examples of others and from my old C...
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...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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...
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)...
1
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
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.