* 和 & 是什么意思?如果参数很复杂,运算符会进行运算吗?

发布于 2024-09-24 15:47:43 字数 378 浏览 5 评论 0原文

简单地说,

&someObject->someAttribute.someMember;

相当于

&(someObject->someAttribute.someMember);

or

(&someObject)->someAttribute.someMember;

or

(&(someObject->someAttribute)).someMember;

或者 为了安全起见,您真的应该在其中添加显式括号吗?

Simply, is

&someObject->someAttribute.someMember;

equivalent to

&(someObject->someAttribute.someMember);

or

(&someObject)->someAttribute.someMember;

or

(&(someObject->someAttribute)).someMember;

Or should you really put explicit parenthesis there just to be safe?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不离久伴 2024-10-01 15:47:43

它相当于:

&((someObject->someAttribute).someMember)

->. 具有相同的优先级并且是左关联的。一元-& 的优先级较低。

如果您有非常复杂的表达式,那么您当然应该使用括号来对事物进行分组并阐明您的代码。不过,这并不是特别复杂。在 C 和 C++ 中类似这样的代码很常见。

It is equivalent to:

&((someObject->someAttribute).someMember)

The -> and . have equal precedence and are left-associative. The unary-& has lower precedence.

If you have very complex expressions, then you should certainly use parentheses to group things and clarify your code. This isn't particularly complex, though; it is quite common to see code like this in C and C++.

酒浓于脸红 2024-10-01 15:47:43

请参阅Wikipedia

向下滚动到运算符部分。图表中较高框中的运算符在较低框中的运算符之前被考虑。另请参阅关联性规则。

或者为了安全起见,您真的应该在其中添加显式括号吗?

这在某种程度上是一个偏好问题,但作为一般规则,如果您在解决问题时遇到困难,那么其他人也会遇到困难。有疑问时请加上括号。

See Wikipedia

Scroll down to the operators section. Operators in a higher box in the chart are considered before operators in a lower box. Also see the rules for associativity.

Or should you really put explicit parenthesis there just to be safe?

This is somewhat of a preference issue but as a general rule, if you're having any trouble figuring it out, then someone else will too. Put parenthesis when in doubt.

深巷少女 2024-10-01 15:47:43

如果您想要 someMember 的地址,则 &(someObject->someAttribute.someMember) 或 &someObject->someAttribute.someMember。

第一个是 someObject->someAttribute,第二个是它的成员 somMember,第三个是 &
地址

结构xxx{
整数xx;
};
结构温度{
整数x;
结构xxx ptr;
};

    struct temp t1 = {125,{127}};
    struct temp *t2 = &t1;
    printf("%d %d",t2->x,*&t2->ptr.xx);

If u want address of the someMember then &(someObject->someAttribute.someMember) OR &someObject->someAttribute.someMember.

First someObject->someAttribute and second to its member somMember and third is &
address

struct xxx{
int xx;
};
struct temp{
int x;
struct xxx ptr;
};

    struct temp t1 = {125,{127}};
    struct temp *t2 = &t1;
    printf("%d %d",t2->x,*&t2->ptr.xx);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文