s [-1] = 0是什么意思?

mar*_*elo 4 c libc

我在学习strtok函数的代码从BSD的libc中,当我在我的机器上运行它,程序接收到的信号SIGSEGVs[-1] = 0.这是代码的链接.

s[-1] = 0吧?

这是我的代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

thi*_*his 6

s[-1]
Run Code Online (Sandbox Code Playgroud)

扩大到:

*( s - 1 )
Run Code Online (Sandbox Code Playgroud)

因此,如果结果指向有效内存,则定义代码.

  • (-1)[s]同样有效.疯狂! (2认同)
  • @HotLicks C肯定有阵列.http://eli.thegreenplace.net/2009/10/21/are-pointers-and-arrays-equivalent-in-c/对数组执行[-1]是未定义的b. (2认同)