gpt4 book ai didi

c - c "Hello world"中的字符串?

转载 作者:行者123 更新时间:2023-11-30 18:27:05 24 4
gpt4 key购买 nike

我不明白第 5 行中的“word”一词是如何打印的。有人能给我解释一下吗?

    #include <stdio.h>
int main(void) {
char str[50] = "hello\0 worl\bd";
printf("\n %s ",str);
printf("%s \n",str+str[4]-*str);
return 0;
}

最佳答案

所以,一步一步:

  • "str" 指向你的字符串"hello\0 worl\bd",它实际上是"hello\0 word" (因为\b 删除前一个字符)

  • *str = 是 char 指针的“内容”,这意味着字符串的第一个字符,即“h”

  • str[4] =是str的第(4+1)个字符,即'o'

  • str[4] - *str = 'o'-'h' = 7 (但为什么是 7?'h' ASCII 字符值为 104,“o”值为 111)

  • str + 7 = str[7]

<小时/>

因此,您基本上是在打印从初始字符串的索引:7 开始的字符串。

因此:'word' ;)

关于c - c "Hello world"中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59751044/

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