Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strtok - char array versus char pointer [duplicate]

Possible Duplicate:
strtok wont accept: char *str

When using the strtok function, using a char * instead of a char [] results in a segmentation fault.

This runs properly:

char string[] = "hello world";
char *result = strtok(string, " ");

This causes a segmentation fault:

char *string = "hello world";
char *result = strtok(string, " ");

Can anyone explain what causes this difference in behaviour?

like image 657
Elle H Avatar asked Nov 03 '10 18:11

Elle H


People also ask

What is the difference between char array and char pointer?

For the array, the total string is stored in the stack section, but for the pointer, the pointer variable is stored into stack section, and content is stored at code section. And the most important difference is that, we cannot edit the pointer type string. So this is read-only.

How do I use Strtok as a pointer?

The first time the strtok() function is called, it returns a pointer to the first token in string1. In later calls with the same token string, the strtok() function returns a pointer to the next token in the string. A NULL pointer is returned when there are no more tokens. All tokens are null-ended.

What is the difference between char * and char []?

What is the difference between char and char*? char[] is a character array whereas char* is a pointer reference. char[] is a specific section of memory in which we can do things like indexing, whereas char* is the pointer that points to the memory location.

What is a char pointer?

char *p = "abc"; defines p with type "pointer to char" and initializes it to point to an object with type "array of char" with length 4 whose elements are initialized with a character string literal. If an attempt is made to use p to modify the contents of the array, the behavior is undefined.


2 Answers

char string[] = "hello world";

This line initializes string to be a big-enough array of characters (in this case char[12]). It copies those characters into your local array as though you had written out

char string[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '\0' };

The other line:

char* string = "hello world";

does not initialize a local array, it just initializes a local pointer. The compiler is allowed to set it to a pointer to an array which you're not allowed to change, as though the code were

const char literal_string[] = "hello world";
char* string = (char*) literal_string;

The reason C allows this without a cast is mainly to let ancient code continue compiling. You should pretend that the type of a string literal in your source code is const char[], which can convert to const char*, but never convert it to a char*.

like image 157
aschepler Avatar answered Sep 23 '22 16:09

aschepler


In the second example:

char *string = "hello world";
char *result = strtok(string, " ");

the pointer string is pointing to a string literal, which cannot be modified (as strtok() would like to do).

You could do something along the lines of:

char *string = strdup("hello world");
char *result = strtok(string, " ");

so that string is pointing to a modifiable copy of the literal.

like image 39
Michael Burr Avatar answered Sep 20 '22 16:09

Michael Burr