"i". Returns-6ren">
gpt4 book ai didi

c - 为什么整数 `j` 返回 `i` ?

转载 作者:行者123 更新时间:2023-11-30 21:42:52 25 4
gpt4 key购买 nike

  //Increment and decrement operators:
char *s = "iLoveC";
int j = 0;
s[j++]; // => "i". Returns the j-th item of s THEN increments value of j.
j = 0;
s[++j]; // => "L". Increments value of j THEN returns j-th value of s.
// same with j-- and --j

关于此的一些问题:

  • s 上的 * 代表什么?
  • 为什么第 4 行 j 返回 i 而不是 0?
  • 为什么第 6 行 j 返回 l 而不是 1?

这来自一个简单的初学者教程( http://learnxinyminutes.com/docs/c/ ),我什至无法理解它。我的背景是 PHP/Javascript/Lisp。

最佳答案

* 运算符将 s 声明为指针。这意味着它将包含分配的变量的地址。这里指针 s 的值为 i,即字符串“iLoveC”的第一个元素。当您使用后增量 s[j++] 时,它相当于 s[0]='i' 但当您使用 s[++j] 时,它相当于 s[1]='L'

查看此链接 http://www.programiz.com/c-programming/c-pointers-arrays并从阅读一些书籍开始。

关于c - 为什么整数 `j` 返回 `i` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30651351/

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