Arrow operator (->) usage in C

后端 未结 12 2057
醉梦人生
醉梦人生 2020-11-22 04:41

I am reading a book called \"Teach Yourself C in 21 Days\" (I have already learned Java and C# so I am moving at a much faster pace). I was reading the chapter on pointers a

12条回答
  •  [愿得一人]
    2020-11-22 05:23

    Yes, that's it.

    It's just the dot version when you want to access elements of a struct/class that is a pointer instead of a reference.

    struct foo
    {
      int x;
      float y;
    };
    
    struct foo var;
    struct foo* pvar;
    pvar = malloc(sizeof(pvar));
    
    var.x = 5;
    (&var)->y = 14.3;
    pvar->y = 22.4;
    (*pvar).x = 6;
    

    That's it!

提交回复
热议问题