Is this notation correct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • johnnash

    Is this notation correct

    typedef astruct
    {
    int x;

    } A;

    struct b

    {
    A a;

    }B;

    struct b *C;

    C = &B;

    so if i want to access say B.a.x, and if i am going to use the
    pointer notation(using C), then is it right to say -

    1. (C->a).x

    2. (*C).a.x
  • johnnash

    #2
    Re: Is this notation correct

    im sorry if i am bugging any of you with my silly questions but the
    thing is im just a intermediate level programmer and my i haven't
    touched C for almost 4 years so it would be nice if some one could
    answer this one and also suggest to me some nice tutorial for revising
    key concepts like the one used in this problem.

    Comment

    • Eric Sosman

      #3
      Re: Is this notation correct

      johnnash wrote:
      typedef astruct
      {
      int x;
      >
      } A;
      >
      struct b
      >
      {
      A a;
      >
      }B;
      >
      struct b *C;
      >
      C = &B;
      >
      so if i want to access say B.a.x, and if i am going to use the
      pointer notation(using C), then is it right to say -
      >
      1. (C->a).x
      >
      2. (*C).a.x
      Both are right. `C->a.x' is also right, and is easier
      to type.

      --
      Eric.Sosman@sun .com

      Comment

      • Richard Heathfield

        #4
        Re: Is this notation correct

        johnnash said:
        typedef astruct
        {
        int x;
        } A;
        struct b
        {
        A a;
        }B;
        struct b *C;
        C = &B;
        >
        so if i want to access say B.a.x, and if i am going to use the
        pointer notation(using C), then is it right to say -
        >
        1. (C->a).x
        Yes, but C->a.x will be fine too. You don't need the parentheses in this
        situation.
        2. (*C).a.x
        That works too. They are synonymous.

        --
        Richard Heathfield <http://www.cpax.org.uk >
        Email: -http://www. +rjh@
        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
        "Usenet is a strange place" - dmr 29 July 1999

        Comment

        • johnnash

          #5
          Re: Is this notation correct

          >
          2. (*C).a.x
          >
          That works too. They are synonymous.
          >

          Would it be correct to remove the bracket in this case as well..i mean

          *C.a.x

          as B = *C

          Comment

          • Richard Heathfield

            #6
            Re: Is this notation correct

            johnnash said:
            >
            >>
            2. (*C).a.x
            >>
            >That works too. They are synonymous.
            >>
            >
            >
            Would it be correct to remove the bracket in this case as well..i mean
            >
            *C.a.x
            >
            as B = *C
            No. That's because . has a higher precedence than *

            Precedence is a way of thinking about how operands bind to operators.
            Because . has a higher precedence than *, *C.a.x is equivalent to *(C.a.x)
            - that is, if x were a pointer, you'd be dereferencing it (and because it
            isn't a pointer, the code is illegal). So, to insist that the * belongs to
            the C object rather than C.a.x, we have to put parentheses around it.

            Alternatively, you can use C->a.x, which is much clearer anyway.

            --
            Richard Heathfield <http://www.cpax.org.uk >
            Email: -http://www. +rjh@
            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
            "Usenet is a strange place" - dmr 29 July 1999

            Comment

            Working...