Velocity Reviews > Is this notation correct

Is this notation correct

johnnash
Guest
Posts: n/a

 02-19-2008
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
Guest
Posts: n/a

 02-19-2008
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.

Eric Sosman
Guest
Posts: n/a

 02-19-2008
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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

Richard Heathfield
Guest
Posts: n/a

 02-19-2008
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

johnnash
Guest
Posts: n/a

 02-19-2008

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

Richard Heathfield
Guest
Posts: n/a

 02-19-2008
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

johnnash
Guest
Posts: n/a

 02-19-2008
On Feb 19, 10:48 pm, Richard Heathfield <(E-Mail Removed)> 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

Eric Sosman
Guest
Posts: n/a

 02-19-2008
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))(.)

--
(E-Mail Removed)

Ben Bacarisse
Guest
Posts: n/a

 02-19-2008
johnnash <(E-Mail Removed)> writes:

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

> typedef astruct

was:

typedef struct a

> {
> int x;
>
> } A;

--
Ben.

Flash Gordon
Guest
Posts: n/a

 02-19-2008
johnnash wrote, On 19/02/08 17:52:
> On Feb 19, 10:48 pm, Richard Heathfield <(E-Mail Removed)> 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