question for char point.

F

Franklin Li

Hi all,

I have one simple program:
#define __EXTENSIONS__
#include <stdio.h>
#include <string.h>

int main() {
char *buf="5/90/45";
char *token;
char *lasts;

printf("tokenizing \"%s\" with strtok():\n", buf);
if ((token = strtok(buf, "/")) != NULL) {
printf("token = "%s\"\n", token);
while ((token = strtok(NULL, "/")) != NULL) {
printf("token = \"%s\"\n", token);
}
}
}

When I compile it using CC and got one Warning: String literal converted to
char* in assignment.
When I run it and got Segmentation Fault(coredump).

Then I changed defination as below:
char str[] = "5/90/45";
char *buf = str;

Then this is no warning and I can get the correct result.

Could you give me the reason?

I think I can use char *point = "string" directly.

Thansk.

Franklin
 
B

baumann@pan

in msdn i got

char *strtok( char *strToken, const char *strDelimit );
wchar_t *wcstok( wchar_t *strToken, const wchar_t *strDelimit );
Parameters

strToken
String containing token(s)
strDelimit
Set of delimiter characters
Libraries

All versions of the C run-time libraries.



All of these functions return a pointer to the next token found in
strToken. They return NULL when no more tokens are found. Each call
modifies strToken

~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~

by substituting a NULL character for each delimiter that is
encountered.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

now you see strtok will modify the content of the first argument.

but your code write

char * buf = "";

which you can not modify its content.

use char buf[] = "" is ok,
because buf 's cotent become modifiable.

baumann@pan
 
B

baumann@pan

when i run your code in rh9, i got Segmentation Fault,
but if your gdb prog, it runs ok, why?
 
B

baumann@pan

when i run your code in rh9, i got Segmentation Fault,
but if run your code in debug mode ,
gdb prog,
and step by step it runs ok,

??????????????????????????????????????????????????????????
??????????????????????why?????????????????????????????????
? ?????????????????????????????????????????????????????????
 
S

S.Tobias

Franklin Li said:
I have one simple program:
#define __EXTENSIONS__
#include <stdio.h>
#include <string.h>
int main() {
char *buf="5/90/45";
Here `buf' points to a static string, which is in read-only
memory. It's okay, until...
char *token;
char *lasts;
printf("tokenizing \"%s\" with strtok():\n", buf);
if ((token = strtok(buf, "/")) != NULL) {
....here: strtok tries to modify the string, which you are not
allowed to do.
You have to make a copy of the string, that can be modified, which....
printf("token = "%s\"\n", token);
while ((token = strtok(NULL, "/")) != NULL) {
printf("token = \"%s\"\n", token);
}
}
}
When I compile it using CC and got one Warning: String literal converted to
char* in assignment.
When I run it and got Segmentation Fault(coredump).
Then I changed defination as below:
char str[] = "5/90/45";
.... you're sort of doing here. `str' is an automatic array ("on stack"),
which you initialize with the string literal (this initialization might
for example copy the string from read-only memory to the new array).
char *buf = str;
Then this is no warning and I can get the correct result.
Could you give me the reason?
I think I can use char *point = "string" directly.

You can use the pointer `point' anyway you want, but not what
it points to.


Another way to change the program would be to malloc() and copy,
and pass the copy to strtok():
char *buf="5/90/45";
char *buf2=malloc(strlen(buf)+1);
/* ... (check) */
strcpy(buf2, buf);
if ((token = strtok(buf2, "/")) != NULL) {
^^^^
 
K

Keith Thompson

S.Tobias said:
Here `buf' points to a static string, which is in read-only
memory. It's okay, until...

...here: strtok tries to modify the string, which you are not
allowed to do.

Actually, it's worse than that. The string literal may or may not be
in read-only memory. Attempting it to modify it invokes undefined
behavior.
 
A

Ari Lukumies

Franklin said:
char *buf="5/90/45";

You are allocating a pointer to a string literal (which may
or may not reside in read-only memory; nevertheless, it's
considered read-only).
if ((token = strtok(buf, "/")) != NULL) {

strtok modifies the contents pointed to by buf, but the contents
are read-only, so modification is not permitted.
Then I changed defination as below:
char str[] = "5/90/45";

Here, you are allocating an array of chars containing the string "5/90/45".
char *buf = str;

And here, you are allocating a pointer that points to that array,
so the contents of the array (str) pointed to by the pointer (buf)
are writable, thus no errors.

You could use 'if ((token = strtok(str, "/")) != NULL)' as well or,
as I'd prefer, 'if (!(token = strtok(str, "/")))'.

-atl-
 
J

john_bode

Franklin said:
Hi all,

I have one simple program:
#define __EXTENSIONS__
#include <stdio.h>
#include <string.h>

int main() {
char *buf="5/90/45";

This declares buf to be a pointer to a string constant. String
constants may or may not be writable, depending on the particular
implementation; in your case, they're not. In general, it's best to
assume they're not.

strtok() has to be able to modify the buffer it's searching (it needs
to be able to replace the delimiters with a 0). Hence your warning and
your core dump (trying to write to non-writable memory).

[snip]
Then I changed defination as below:
char str[] = "5/90/45";
char *buf = str;

str is a statically allocated array of char, implicitly sized based on
the initializer, and the contents of the initializer are copied to it.
The contents of str are writable.
Then this is no warning and I can get the correct result.

Could you give me the reason?

I think I can use char *point = "string" directly.

You can when you aren't trying to modify the string.
 
B

Barry Schwarz

This declares buf to be a pointer to a string constant. String
constants may or may not be writable, depending on the particular
implementation; in your case, they're not. In general, it's best to
assume they're not.

Whether they are physically writable or not may be an implementation
detail but any attempt to modify a string literal invokes undefined
behavior.



<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top