Using pointer to character as the argument of strtok

Hitokiri :

I try to split the string by using strtok function. But the program become failed if i use the pointer to character as the argument of this function.

If i initialize the string as s2 or s3 the program works well. But if i use pointer to character as s1 the program get Segmentation fault (core dumped).

char *s1 = "1A 2B 3C 4D";
char s2[] = "1A 2B 3C 4D";
char s3[20] = "1A 2B 3C 4D";

The problem is the other functions, printf and strlen work without failure, but only strtok get error.

The complete code below:

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

void split_string(char *s) {
    char * token = strtok(s," ");
    while (token != NULL) {
        printf("%s\n", token);
        token = strtok(NULL, " ");
    }
}

int main()
{
    char *s1 = "1A 2B 3C 4D";
    char s2[] = "1A 2B 3C 4D";
    char s3[20] = "1A 2B 3C 4D";
    printf("size of s1 = %ld, s2 = %ld, s3 = %ld\n", strlen(s1), strlen(s2), strlen(s3));
    printf("s1: %s\ns2: %s\ns3: %s\n",s1,s2,s3);
    printf("split s2: \n");
    split_string(s2);
    printf("split s3: \n");
    split_string(s3);
    printf("split s1: \n");
    split_string(s1);
    return 0;
}

The result after running:

size of s1 = 11, s2 = 11, s3 = 11
s1: 1A 2B 3C 4D
s2: 1A 2B 3C 4D
s3: 1A 2B 3C 4D
split s2: 
1A
2B
3C
4D
split s3: 
1A
2B
3C
4D
split s1: 
Segmentation fault (core dumped)

strtok from man page: char *strtok(char *str, const char *delim);

Please help to understand this problem.

snr :

Battousai, firstly you need to use your reverse side of the katana to achieve your aim by using readable/writable area. If you don't do this, unless the compiler/OS(Kamiya Kaoru) doesn't prevent you, Shishio Makoto may ruin guys important for you and around you via Sojiro Seta, living in your memory such as Sanosuke Sagara, Yahiko Myojin.

strtok writes into the string you give it - overwriting the separator character with null and keeping a pointer to the rest of the string.

char *s1 = "1A 2B 3C 4D"; // you have a pointer to some read-only characters
char s2[] = "1A 2B 3C 4D"; // same, decay into pointer
char s3[20] = "1A 2B 3C 4D"; // a twenty element array of characters that you can do what you like with.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Printing command line argument tokens using strtok function. How to return a pointer to pointer using this function

invalid pointer when using strtok_r

Toggle a character using a pointer

Store integer using character pointer

Getting incorrect values when accessing variables passed along in a pointer to a character array for strtok

Run-time error storing pointer character from strtok after multiple calls

Passing the right argument to strtok

The strtok Function With char * Argument

How to produce a character array in ctypes and pass its pointer as a function argument

Clang bug? Using pointer as template argument

Dangers of using reference-to-pointer as function argument

function template deduction using raw pointer as argument

using an absolute pointer address as a template argument

Using the null terminating character as null pointer in C

Print Integer value using character pointer in c

Using character vs. pointer for an "array of words"

Constant character pointer using unique_ptr

How to access character inside char * using pointer

how to access character pointer value using ctypes?

Using Table to Group: invalid 'type' (character) of argument

PHP using " character inside string argument

splitting string with strtok to double pointer

strtok in C crashes with char pointer

Pointer as an argument in function pointer

Comparing a pointer to a pointer with a character?

using strtok and populating array

Delimiting path using strtok()

Segfault when using strtok

Using reflect to update value by reference when argument is not a pointer in go