# Is this notation correct

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

1. ### johnnashGuest

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

2. ### johnnashGuest

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

3. ### Eric SosmanGuest

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
4. ### Richard HeathfieldGuest

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@
"Usenet is a strange place" - dmr 29 July 1999

Richard Heathfield, Feb 19, 2008
5. ### johnnashGuest

>
> > 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
6. ### Richard HeathfieldGuest

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@
"Usenet is a strange place" - dmr 29 July 1999

Richard Heathfield, Feb 19, 2008
7. ### johnnashGuest

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@
> "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
8. ### Eric SosmanGuest

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
9. ### Ben BacarisseGuest

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
10. ### Flash GordonGuest

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

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

Flash Gordon, Feb 19, 2008
11. ### Richard TobinGuest

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

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