Unexpected error with strtok function with pointers in c

I'm just trying to understand how the function works strtok()

:

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

int main(){
    char* p="abc,def,ghi,jkl,mno,pqr,stu,vwx,yz";
    char* q=strtok(p, ",");
    printf("%s", q);

    return 0;
}

      

I tried to run this simple code, but I keep getting weird error that I can't figure out at all. This is the error I keep getting:

screen shot

This is probably not relevant, but due to the fact that my command prompt keeps coming out, I changed some parameters in my project and added this line Console (/SUBSYSTEM:CONSOLE)

somewhere and I tried to run the program without debugging at first, which didn't even start the program. then i tried it with debug and how i got the error in the link.

+3


source to share


4 answers


Instead

char* p="abc,def,ghi,jkl,mno,pqr,stu,vwx,yz";

      

using

char p[] = "abc,def,ghi,jkl,mno,pqr,stu,vwx,yz";

      



as according to the man page strtok()

it can change the input line. A string literal in your code read-only

. Therefore, a segmentation fault occurs.

Related link:

Be careful when using these features. If you use them, please note that:

* These functions change their first argument.

+3


source


char* p="abc,def,ghi,jkl,mno,pqr,stu,vwx,yz";

assigns persistent memory to p

. In fact, the type p

should be const char*

.

Trying to change this line in any way gives undefined behavior. Therefore, strtok

it is very bad to use for this. To fix this use



char p[]="abc,def,ghi,jkl,mno,pqr,stu,vwx,yz";

      

This allocates a row on the stack and modifications to it are allowed. p

decays to a pointer in certain cases; one of which is a parameter strtok

.

+2


source


You cannot use string literals with the strtok function because the function is trying to modify the string passed to it as an argument.

Any attempt to modify a string literal results in undefined behavior.

According to the C standard (6.4.5 string literals)

7 It is not known if these arrays are different if their elements have corresponding values. If the program tries to modify such an array, the behavior is undefined.

Change this definition

char* p="abc,def,ghi,jkl,mno,pqr,stu,vwx,yz";

      

to

char s[] = "abc,def,ghi,jkl,mno,pqr,stu,vwx,yz";
//...
char* q = strtok( s, "," );

      

For example, the main function might look like

int main()
{
    char s[] = "abc,def,ghi,jkl,mno,pqr,stu,vwx,yz";
    char* q = strtok( s, "," );

    while ( q != NULL )
    {
        puts( q );
        strtok( NULL, "," );
    }

    return 0;
}

      

+2


source


First, to solve the error: Segmentation fault

.
Change char *p

to char p[]

.

The reason is that it strtok()

will add '\0'

a line separator when a bullet character is found. In your case, it char* p="abc,def,ghi,jkl,mno,pqr,stu,vwx,yz";

is stored in the read-only section of the program as a string literal.

Since you are trying to change the read-only location results in Segmentation fault

+2


source







All Articles