Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does s[-1] = 0 mean?

Tags:

c

libc

I'm studying the code of the function strtok from bsd's libc, when I ran it in my machine, the program received signal SIGSEGV in s[-1] = 0. Here's the link to the code.

Is s[-1] = 0 right?

This is my code:

#include <stdio.h>
#include <stdlib.h>
#include "strtok.c"

int main(int argc, char* argv[]) {
    char* str = "xxxx xxxyy fdffd";
    const char* s = " ";

    char* token = strtok(str, s);

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

    return 0;
}
like image 340
marcelo Avatar asked Dec 09 '22 10:12

marcelo


1 Answers

s[-1]

Is expanded to:

*( s - 1 )

Therefore, if the result points to valid memory, the code is defined.

like image 113
this Avatar answered Dec 20 '22 04:12

this