Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is this notation correct

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
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.

 
Reply With Quote
 
 
 
 
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)
 
Reply With Quote
 
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@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
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
 
Reply With Quote
 
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@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
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@
> 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
 
Reply With Quote
 
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)
 
Reply With Quote
 
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.
 
Reply With Quote
 
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
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert Infix notation to postfix notation Tameem C Programming 454 01-31-2014 06:01 PM
Hungarian Notation Vs. Pascal Notation? Grey Squirrel ASP .Net 6 03-21-2007 09:42 AM
correct or not correct? Dan HTML 7 10-02-2003 10:16 PM
To correct my program. please, check to find errors and correct me. joon Java 1 07-08-2003 06:13 AM
Dot notation V Bracket notation Robert Mark Bram Javascript 3 07-05-2003 03:59 AM



Advertisments