Arrow operator (->) usage in C

后端 未结 12 2051
醉梦人生
醉梦人生 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:15
    #include<stdio.h>
    struct examp{
    int number;
    };
    struct examp a,*b=&a;`enter code here`
    main()
    {
    a.number=5;
    /* a.number,b->number,(*b).number produces same output. b->number is mostly used in linked list*/
       printf("%d \n %d \n %d",a.number,b->number,(*b).number);
    }
    

    output is 5 5 5

    0 讨论(0)
  • 2020-11-22 05:19

    I had to make a small change to Jack's program to get it to run. After declaring the struct pointer pvar, point it to the address of var. I found this solution on page 242 of Stephen Kochan's Programming in C.

    #include <stdio.h>
    
    int main()
    {
      struct foo
      {
        int x;
        float y;
      };
    
      struct foo var;
      struct foo* pvar;
      pvar = &var;
    
      var.x = 5;
      (&var)->y = 14.3;
      printf("%i - %.02f\n", var.x, (&var)->y);
      pvar->x = 6;
      pvar->y = 22.4;
      printf("%i - %.02f\n", pvar->x, pvar->y);
      return 0;
    }
    

    Run this in vim with the following command:

    :!gcc -o var var.c && ./var
    

    Will output:

    5 - 14.30
    6 - 22.40
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-11-22 05:27

    foo->bar is equivalent to (*foo).bar, i.e. it gets the member called bar from the struct that foo points to.

    0 讨论(0)
  • 2020-11-22 05:29
    #include<stdio.h>
    
    int main()
    {
        struct foo
        {
            int x;
            float y;
        } var1;
        struct foo var;
        struct foo* pvar;
    
        pvar = &var1;
        /* if pvar = &var; it directly 
           takes values stored in var, and if give  
           new > values like pvar->x = 6; pvar->y = 22.4; 
           it modifies the values of var  
           object..so better to give new reference. */
        var.x = 5;
        (&var)->y = 14.3;
        printf("%i - %.02f\n", var.x, (&var)->y);
    
        pvar->x = 6;
        pvar->y = 22.4;
        printf("%i - %.02f\n", pvar->x, pvar->y);
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-22 05:29

    Dot is a dereference operator and used to connect the structure variable for a particular record of structure. Eg :

    struct student
        {
          int s.no;
          Char name [];
          int age;
        } s1,s2;
    
    main()
        {
          s1.name;
          s2.name;
        }
    

    In such way we can use a dot operator to access the structure variable

    0 讨论(0)
提交回复
热议问题