指针和结构

Q-b*_*uit 1 c++

我试图围绕指针,参考和地址,但每次我认为我得到了一些意想不到的东西弹出.

为什么我们不需要取消引用结构来设置此示例中的值?

// pointer_tet.cpp
 #include <iostream>
struct example
{
    char name[20];
    int number;
};
int main()
{
   using namespace std;
   example anExample = {"Test", 5};
   example * pt = &anExample;
   pt->number = 6;
   cout << pt->number << endl;

   int anotherExample = 5;
   int * pd = &anotherExample;
   *pd = 6;
   cout << *pd << endl;

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

编辑:谢谢你的回答!令我困惑的是无法设置*pt.number = 6.

Jos*_*eld 8

正在解除引用pt.你在做:

pt->number = 6;
Run Code Online (Sandbox Code Playgroud)

这相当于:

(*pt).number = 6;
Run Code Online (Sandbox Code Playgroud)

->运营商提供了一个便捷的方式,通过一个指针来访问成员.