我写的时候不能完全理解语言的作用
*(t++)
*t++
什么时候t
是指针?
最佳答案
这两个表达式
*(t++)
*t++
由于运算符的优先级,它们是等价的。
所以后缀运算符++的优先级高于一元运算符*。
后缀运算符++ 的结果是其操作数在递增之前的值。
来自 C 标准(6.5.2.4 后缀递增和递减运算符)
2 The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it)....
如果你有这样的指针,请考虑由于指针运算
T *p;
其中 T
是某种类型,然后在操作 p++
之后,指针本身的最终值将增加 sizeof( T )
.对于类型 char sizeof( char )
总是等于 1
。
考虑以下演示程序。
#include <stdio.h>
int main(void)
{
char *t = "AB";
printf( "%c\n", *t++ );
printf( "%c\n", *t );
return 0;
}
它的输出是
A
B
你可以替换这个语句
printf( "%c\n", *t++ );
对于这个语句
printf( "%c\n", *( t++ ) );
你会得到相同的结果。
其实就是这个表达式
*(t++)
也等同于表达式
t++[0]
关于c - 难以理解指针操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49694028/