why this code dumps core?

V

v4vijayakumar

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, token);
++i;
token = strtok(value, " ,\t");
}

return 0;
}
 
I

Ian Collins

v4vijayakumar said:
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.
 
N

Nelu

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, token);
++i;
token = strtok(value, " ,\t");
}

return 0;
}


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

Keith Thompson

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

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

Martin Ambuhl

v4vijayakumar said:
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.
 
K

Keith Thompson

Martin Ambuhl said:
v4vijayakumar said:
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.
 
C

Charles Richmond

Nelu said:
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, 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.
 

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,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top