繁体   English   中英

C 中结构取消引用运算符的更复杂的表示法,->?

[英]The more complex notation of the Structure Dereference operator in C, ->?

我在这里有一个考试问题,它问:

“C 运算符 -> 是更复杂符号的简写。解释使用 -> 或更复杂符号的情况。写一个更复杂符号的示例。”

我不确定考官在这里要找什么。 我的印象是只有一种方法可以表示结构取消引用,那就是 ->。 任何人都可以帮忙吗?

取消引用可以通过使用->(*). . 看例子

struct point {
  int x;
  int y;
};
struct point my_point = { 3, 7 };
struct point *p = &my_point;  /* To declare and define p as a pointer of type struct point,
                                 and initialize it with the address of my_point. */

(*p).x = 8;                   /* To access the first member of the struct */
p->x = 8;    

很可能你的老师要求(*p).x = 8; .

->语法是(*).的简写(*). 语法,所以struct->member等价于(*struct).member

由于运算符优先级,括号是强制性的,基本上在这里成员访问( . )运算符比取消引用( * )运算符具有更高的优先级。

请记住,程序员很懒惰,而且通常喜欢捷径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM