By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,929 Members | 634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,929 IT Pros & Developers. It's quick & easy.

question for char point.

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

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

Franklin Li wrote:
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


Nov 14 '05 #2

P: n/a
when i run your code in rh9, i got Segmentation Fault,
but if your gdb prog, it runs ok, why?

Nov 14 '05 #3

P: n/a
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????????????????????????? ????????
? ?????????????????????????????????????????????????? ???????

Nov 14 '05 #4

P: n/a
Franklin Li <pe*******@hotmai.com> wrote:
Hi all, 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) {
^^^^

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #5

P: n/a
"S.Tobias" <si***@FamOuS.BedBuG.pAlS.INVALID> writes:
Franklin Li <pe*******@hotmai.com> wrote:

[...]
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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6

P: n/a
Franklin Li wrote:
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-
--
A multiverse is figments of its own creations
Nov 14 '05 #7

P: n/a


Franklin Li wrote:
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.

Nov 14 '05 #8

P: n/a
On 4 Jun 2005 14:57:30 -0700, jo*******@my-deja.com wrote:


Franklin Li wrote:
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.


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>>
Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.