gpt4 book ai didi

c - 为什么 C 代码中的输出从数字 1 而不是 0 开始计算数组索引?

转载 作者:太空狗 更新时间:2023-10-29 17:05:24 28 4
gpt4 key购买 nike

所以我在 C 代码中得到了这个字符串,我从一本书中得到了一个引用,后面是另一个字符串,其中有一个来自该引用的单词。当它告诉程序查找子串的位置时,它从数字 1 而不是 0 开始计数,这是为什么?这是我的意思:

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

int main()
{
char str[]="No Time like the Present";
char sub[]="Time";
if (strstr(str, sub)== NULL)
{
printf("not found");
}
else
{
printf("Index number found at %d",strstr(str,sub)-str);
}
return 0
}

所以它会说: 在第 3 处找到索引

但它不应该打印在数字 2 处找到的索引,因为你是从零开始的吗?或者你有时可以从数字 1 开始吗??!

最佳答案

不,它从零开始:

No Time...
^^^^
0123

关于c - 为什么 C 代码中的输出从数字 1 而不是 0 开始计算数组索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18206563/

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