Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Operand of postfix operator ->

Reply
Thread Tools

Operand of postfix operator ->

 
 
Rajesh S R
Guest
Posts: n/a
 
      04-24-2007
Consider the following code:
int main()
{
struct name
{
long a;
int b;
long c;
}s={3,4,5},*p;
p=&s;

printf("%d",*(int *)((char *)p+(unsigned int)&(((struct name*)0)-
>b)));


return 0;
}

Is the expression (unsigned int)&(((struct name*)0)->b) valid as per
C99?

That is,
Can an operand of postfix operator -> be a null pointer?

Please clarify.

Thanks in advance for the reply.

 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      04-24-2007
In article <(E-Mail Removed) .com>,
Rajesh S R <(E-Mail Removed)> wrote:

>Is the expression (unsigned int)&(((struct name*)0)->b) valid as per
>C99?


No.

>That is,
>Can an operand of postfix operator -> be a null pointer?


-> is not a postfix operator. It has two operands and appears between
them, so it's an infix operator.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      04-24-2007
Rajesh S R wrote:

> Consider the following code:
> int main()
> {
> struct name
> {
> long a;
> int b;
> long c;
> }s={3,4,5},*p;
> p=&s;


Were we pairing, I'd not let that stay like that. Oh no.

> printf("%d",*(int *)((char *)p+(unsigned int)&(((struct name*)0)-
>>b)));


At this point I'd ask you what on earth you were trying to do.
I wouldn't much care whether it was legal-by-the-Standard; I'd
care that it was indecently obscure.

> return 0;
> }
>
> Is the expression (unsigned int)&(((struct name*)0)->b) valid as per
> C99?


I don't think so.

> That is,
> Can an operand of postfix operator -> be a null pointer?


Yes, it can be. The result is undefined. So don't do that.

(Your clarification asks a different question from your original.)

--
Nit-picking is best done among friends.

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      04-24-2007
In article <f0kv5l$isv$(E-Mail Removed)>,
Chris Dollin <(E-Mail Removed)> wrote:

>> printf("%d",*(int *)((char *)p+(unsigned int)&(((struct name*)0)-
>>>b)));


>At this point I'd ask you what on earth you were trying to do.


Presumably he's trying to implement sizeof(), as apparently everyone
in India is doing these days. I assume he's on the same course as all
the others asking similar questions.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      04-24-2007
"Rajesh S R" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> printf("%d",*(int *)((char *)p+(unsigned int)&(((struct name*)0)-
>>b)));

....
> Is the expression (unsigned int)&(((struct name*)0)->b) valid as per
> C99?


No. What you're seeing is one rather common implementation of offsetof(),
but it's officially UB for _you_ to use this construct unless you're writing
the implementation itself. Use offsetof() and you don't have to worry about
those systems where it doesn't actually work.

> That is,
> Can an operand of postfix operator -> be a null pointer?


It's an infix operator, and the first operand must be a pointer to an
object. Since a null pointer constant does not point to an object by
definition, it's UB.

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Rajesh S R
Guest
Posts: n/a
 
      04-24-2007
Thanks for ur reply.

> -> is not a postfix operator. It has two operands and appears between
> them, so it's an infix operator.


But C standards categorises an -> as postfix operator( C99 6.5.2 ) .
So what's wrong in stating it as a postfix operator?
Even C99 calls it that way!

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      04-24-2007
In article <(E-Mail Removed) .com>,
Rajesh S R <(E-Mail Removed)> wrote:

>> -> is not a postfix operator. It has two operands and appears between
>> them, so it's an infix operator.


>But C standards categorises an -> as postfix operator( C99 6.5.2 ) .


You're right.

I can't imagine why they decided to misuse a well-defined term in that
way.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
Rajesh S R
Guest
Posts: n/a
 
      04-24-2007
Thanks for your reply.

> -> is not a postfix operator. It has two operands and appears between
> them, so it's an infix operator.


(C99 6.5.2) categorises -> as postfix operator. So whats wrong in
calling it that way?

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      04-24-2007
Rajesh S R wrote:
> Consider the following code:
> int main()
> {
> struct name
> {
> long a;
> int b;
> long c;
> }s={3,4,5},*p;
> p=&s;
>
> printf("%d",*(int *)((char *)p+(unsigned int)&(((struct name*)0)-
>> b)));

>
> return 0;
> }
>
> Is the expression (unsigned int)&(((struct name*)0)->b) valid as per
> C99?


There are otherwise enough errors in your code to make "valid as per
C99" a sick joke. And this is the most perverse way I have ever seen of
spelling the expression "p->b".

> That is,
> Can an operand of postfix operator -> be a null pointer?

-> is not a postfix operator. Strangely, your code claims it isn't a
token at all.
 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      04-24-2007
"Rajesh S R" <(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed) oups.com...
> Consider the following code:
> int main()
> {
> struct name
> {
> long a;
> int b;
> long c;
> }s={3,4,5},*p;
> p=&s;
>
> printf("%d",*(int *)((char *)p+(unsigned int)&(((struct name*)0)-
>>b)));

You've managed to beat my lecturer (and the book he copied from) in the
contest of the ugliest line of code ever:
http://groups.google.com/group/comp.lang.c/msg/f0495c8e34d3016?as_umsgid=evt7mc$d5o$(E-Mail Removed) t

> return 0;
> }
>
> Is the expression (unsigned int)&(((struct name*)0)->b) valid as per
> C99?

What the hell are you trying to do?


 
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
postfix operator++(int) with const Chris Forone C++ 7 06-20-2008 06:25 PM
postfix vs prefix operator subramanian100in@yahoo.com, India C++ 8 07-18-2007 06:09 AM
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) aarthi28@gmail.com C++ 29 06-21-2007 08:42 PM
operand of postfix operator-> Rajesh S R C Programming 1 04-24-2007 02:05 PM
Unsupported error,& Right operand of "Divide" operator must be a power of 2.. Kim JM VHDL 1 04-02-2004 06:58 PM



Advertisments