Is this notation correct

Discussion in 'C Programming' started by johnnash, Feb 19, 2008.

  1. johnnash

    johnnash Guest

    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, Feb 19, 2008
    #1
    1. Advertising

  2. johnnash

    johnnash Guest

    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.
     
    johnnash, Feb 19, 2008
    #2
    1. Advertising

  3. johnnash

    Eric Sosman Guest

    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, Feb 19, 2008
    #3
  4. 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
     
    Richard Heathfield, Feb 19, 2008
    #4
  5. johnnash

    johnnash Guest


    >
    > > 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
     
    johnnash, Feb 19, 2008
    #5
  6. 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
     
    Richard Heathfield, Feb 19, 2008
    #6
  7. johnnash

    johnnash Guest

    On Feb 19, 10:48 pm, Richard Heathfield <> wrote:
    > 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



    so *(C.a.x) basically means that

    C points to a and a has a member called x which contains address of an
    int. So in order to obtain that value of int, we need to dereference
    it using *. Is that right ?

    btw thanks for help richard
     
    johnnash, Feb 19, 2008
    #7
  8. johnnash

    Eric Sosman Guest

    johnnash wrote:
    >
    > so *(C.a.x) basically means that
    >
    > C points to a and a has a member called x which contains address of an
    > int. So in order to obtain that value of int, we need to dereference
    > it using *. Is that right ?


    No. It means that C *is* a struct or union with an element
    named a, and that a in turn *is* a struct or union with an
    element named x, and that x is a pointer to something. The
    entire expression says to find the a inside the C and then
    the x inside that a, and to refer to the thing the x points at.

    The ((parentheses)) (are (unnecessary))(.)

    --
     
    Eric Sosman, Feb 19, 2008
    #8
  9. johnnash <> writes:

    I think the replies you've had have assumed that:

    > typedef astruct


    was:

    typedef struct a

    > {
    > int x;
    >
    > } A;


    --
    Ben.
     
    Ben Bacarisse, Feb 19, 2008
    #9
  10. johnnash

    Flash Gordon Guest

    johnnash wrote, On 19/02/08 17:52:
    > On Feb 19, 10:48 pm, Richard Heathfield <> wrote:
    >> johnnash said:


    <snip>

    >> 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.


    Even if x was a pointer it would still be illegal since C is a pointer
    and you can't use a . on a pointer. By only mentioning x not being a
    pointer I think you confused the OP.

    >> Alternatively, you can use C->a.x, which is much clearer anyway.
    >>
    >> --
    >> Richard Heathfield <http://www.cpax.org.uk>


    Please don't quote peoples signatures, the bit typically after the
    "-- ".

    > so *(C.a.x) basically means that
    >
    > C points to a and a has a member called x which contains address of an
    > int. So in order to obtain that value of int, we need to dereference
    > it using *. Is that right ?


    No, it means that C has a member called a which has a member called x
    which you read and dereference.

    I agree with Richard that you should always use -> when you have a
    pointer to a struct and want to access a member of the pointed to
    struct. It makes it *much* easier which is almost certainly why it was
    added to the language.
     
    Flash Gordon, Feb 19, 2008
    #10
  11. In article <-gordon.me.uk>,
    Flash Gordon <> wrote:

    >I agree with Richard that you should always use -> when you have a
    >pointer to a struct and want to access a member of the pointed to
    >struct. It makes it *much* easier which is almost certainly why it was
    >added to the language.


    The -> operator could originally be applied to any address, and the
    struct declaration effectively just associated offsets and types with
    names. It may even have predated the . operator.

    -- Richard
    --
    :wq
     
    Richard Tobin, Feb 20, 2008
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. joon
    Replies:
    1
    Views:
    516
    Roedy Green
    Jul 8, 2003
  2. Dan

    correct or not correct?

    Dan, Oct 2, 2003, in forum: HTML
    Replies:
    7
    Views:
    445
  3. Grey Squirrel

    Hungarian Notation Vs. Pascal Notation?

    Grey Squirrel, Mar 19, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    1,316
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Mar 21, 2007
  4. Tameem
    Replies:
    454
    Views:
    12,027
  5. Robert Mark Bram

    Dot notation V Bracket notation

    Robert Mark Bram, Jul 4, 2003, in forum: Javascript
    Replies:
    3
    Views:
    471
    Robert Mark Bram
    Jul 5, 2003
Loading...

Share This Page