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

why this code dumps core?

P: n/a
why the following code dumps core? TIA.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *agrv[])
{
int i = 0;
typedef char ca_t[20];
ca_t tokens[10];
char *token;
char *value = "hhjhjkh, ashdkja, dmnas,lkjlk";

token = strtok(value, " ,\t");
while(token != NULL)
{
strcpy(tokens[i], token);
++i;
token = strtok(value, " ,\t");
}

return 0;
}

Jun 20 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
v4vijayakumar wrote:
why the following code dumps core? TIA.

For the same reason as the code in the thread "Why occur the mistake in
runtime about strtok()?" from a couple of days ago.

--
Ian Collins.
Jun 20 '06 #2

P: n/a
"v4vijayakumar" <v4***********@yahoo.com> writes:

F> why the following code dumps core? TIA.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *agrv[])
{
int i = 0;
typedef char ca_t[20];
ca_t tokens[10];
char *token;
char *value = "hhjhjkh, ashdkja, dmnas,lkjlk";

token = strtok(value, " ,\t");
while(token != NULL)
{
strcpy(tokens[i], token);
++i;
token = strtok(value, " ,\t");
}

return 0;
}


You cannot modify a string literal and strtok modifies its argument.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jun 20 '06 #3

P: n/a
Nelu <sp*******@gmail.com> writes:
"v4vijayakumar" <v4***********@yahoo.com> writes: [...] You cannot modify a string literal and strtok modifies its argument.


<PEDANTIC>
strtok modifies the string to which its argument points.
</PEDANTIC>

--
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.
Jun 20 '06 #4

P: n/a
Keith Thompson <ks***@mib.org> writes:
Nelu <sp*******@gmail.com> writes:
"v4vijayakumar" <v4***********@yahoo.com> writes:

[...]
You cannot modify a string literal and strtok modifies its argument.


<PEDANTIC>
strtok modifies the string to which its argument points.
</PEDANTIC>


Good point.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jun 20 '06 #5

P: n/a
v4vijayakumar wrote:
why the following code dumps core? TIA.
[...] char *value = "hhjhjkh, ashdkja, dmnas,lkjlk";

token = strtok(value, " ,\t");


Because strtok modifies the contents of the string pointed to by its
first argument, and value points to a (non-modifiable) string literal.
Jun 20 '06 #6

P: n/a
Martin Ambuhl <ma*****@earthlink.net> writes:
v4vijayakumar wrote:
why the following code dumps core? TIA.


[...]
char *value = "hhjhjkh, ashdkja, dmnas,lkjlk";
token = strtok(value, " ,\t");


Because strtok modifies the contents of the string pointed to by its
first argument, and value points to a (non-modifiable) string literal.


Yes. It's non-modifiable in the sense that attempting to modify it
invokes undefined behavior, which the compiler isn't required to warn
you about.

You should think of string literals as being constant, even though
they're not really, sort of. If you had declared "value" as:

const char *value = "hhjhjkh, ashdkja, dmnas,lkjlk";

the compiler probably would have given you a warning.

--
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.
Jun 20 '06 #7

P: n/a
Nelu wrote:

"v4vijayakumar" <v4***********@yahoo.com> writes:

F> why the following code dumps core? TIA.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *agrv[])
{
int i = 0;
typedef char ca_t[20];
ca_t tokens[10];
char *token;
char *value = "hhjhjkh, ashdkja, dmnas,lkjlk";

token = strtok(value, " ,\t");
while(token != NULL)
{
strcpy(tokens[i], token);
++i;
token = strtok(value, " ,\t");
}

return 0;
}


You cannot modify a string literal and strtok modifies its argument.

And if you fix the above problem, you need to pass NULL as the first
argument to "strtok()" *after* the initial call. Otherwise, you will
end up with an infinite loop and without the result you seek.

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Jun 21 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.