gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:13:01 27 4
gpt4 key购买 nike

我正在研究 bsd 的 libc 中函数 strtok 的代码,当我在我的机器上运行它时,程序在 s[-1] = 0 中接收到信号 SIGSEGV。这是 link到代码。

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;
}

最佳答案

s[-1]

扩展为:

*( s - 1 )

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

关于c - s[-1] = 0 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21341763/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com