Is this notation correct

J

johnnash

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
 
J

johnnash

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

Eric Sosman

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

2. (*C).a.x

Both are right. `C->a.x' is also right, and is easier
to type.
 
R

Richard Heathfield

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

Richard Heathfield

johnnash said:
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.
 
J

johnnash

johnnash said:





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
 
E

Eric Sosman

johnnash said:
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))(.)
 
F

Flash Gordon

johnnash wrote, On 19/02/08 17:52:
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.

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

Richard Tobin

Flash Gordon said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top