473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Operand of postfix operator ->

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.

Apr 24 '07 #1
19 2475
In article <11**********************@n35g2000prd.googlegroups .com>,
Rajesh S R <SR**********@gmail.comwrote:
>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.
Apr 24 '07 #2
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

Apr 24 '07 #3
In article <f0**********@murdoch.hpl.hp.com>,
Chris Dollin <ch**********@hp.comwrote:
>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.
Apr 24 '07 #4
"Rajesh S R" <SR**********@gmail.comwrote in message
news:11**********************@n35g2000prd.googlegr 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

Apr 24 '07 #5
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!

Apr 24 '07 #6
In article <11**********************@b40g2000prd.googlegroups .com>,
Rajesh S R <SR**********@gmail.comwrote:
>-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.
Apr 24 '07 #7
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?

Apr 24 '07 #8
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.
Apr 24 '07 #9
"Rajesh S R" <SR**********@gmail.comha scritto nel messaggio
news:11**********************@n35g2000prd.googlegr 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/f0************************************@tdi.cu.mi.i t
return 0;
}

Is the expression (unsigned int)&(((struct name*)0)->b) valid as per
C99?
What the hell are you trying to do?
Apr 24 '07 #10
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <11**********************@b40g2000prd.googlegroups .com>,
Rajesh S R <SR**********@gmail.comwrote:
>>-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.
Probably because the thing on the right hand side isn't really an
operand; it must be the name of a member of the appropriate structure
or union. In s->m, s must be an expression (specifically a
postfix-expression), but m *cannot* be an expression.

The way I sometimes think of it (which is not the way the standard
expresses it) is that a declaration like:

struct foo { int x; int y; };

implicitly creates *four* postfix operators: ".x" and ".y", which can
be applied to expresions of type struct foo, and "->x" and "->y",
which can be applied to expressions of type struct foo*.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 24 '07 #11
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>I can't imagine why they decided to misuse a well-defined term in that
way.
>Probably because the thing on the right hand side isn't really an
operand; it must be the name of a member of the appropriate structure
or union. In s->m, s must be an expression (specifically a
postfix-expression), but m *cannot* be an expression.
This argument would be a lot more plausible if they didn't also call
the array subscripting operator postfix. It's particular egregious in
that case as the subscripting operator is commutative!

I suppose they just considered that these operators are "accessors",
where the "main" operand is (usually) on the right.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Apr 24 '07 #12
Rajesh S R <SRRajesh1...@gmail.comwrote:
>
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?
Yes, the expression is defined and the left operand of -can be a
null pointer.

We all know that dereferencing a null pointer produces undefined
behavior, but if the -operator is not evaluated, it will not.

One such example with defined behavior is: sizeof( ((struct name*)0)-
>b ), another is your expression.
There exists a special rule in C99:

"If the operand (of the unary & operator) is the result of a unary *
operator, neither that operator nor the & operator is evaluated and
the result is as if both were omitted"

The expression &(((struct name*)0)->b) can be re-written as
&((*((struct name*)0)).b) and thus the above rule can be applied -
neither the & nor the * operator will be evaluated. No evaluation then
means no undefined behavior.

Apr 25 '07 #13
whyglinux wrote, On 25/04/07 05:55:
Rajesh S R <SRRajesh1...@gmail.comwrote:
>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?

Yes, the expression is defined and the left operand of -can be a
null pointer.

We all know that dereferencing a null pointer produces undefined
behavior, but if the -operator is not evaluated, it will not.
I disagree, see below.
One such example with defined behavior is: sizeof( ((struct name*)0)-
>b ), another is your expression.

There exists a special rule in C99:

"If the operand (of the unary & operator) is the result of a unary *
operator, neither that operator nor the & operator is evaluated and
the result is as if both were omitted"

The expression &(((struct name*)0)->b) can be re-written as
&((*((struct name*)0)).b) and thus the above rule can be applied -
neither the & nor the * operator will be evaluated. No evaluation then
means no undefined behavior.
The operand is not the result of a unary *, it is the result of
selecting an element of a struct *after* dereferencing the unary * to
find the relevant instance of the struct. Therefore that exception does
not apply.

There is also still the question "why not use the offsetof macro? that's
what it's there fore."
--
Flash Gordon
Apr 25 '07 #14
Flash Gordon <s...@flash-gordon.me.ukwrote:
whyglinux wrote, On 25/04/07 05:55:
Rajesh S R <SRRajesh1...@gmail.comwrote:
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?
Yes, the expression is defined and the left operand of -can be a
null pointer.
We all know that dereferencing a null pointer produces undefined
behavior, but if the -operator is not evaluated, it will not.

I disagree, see below.
One such example with defined behavior is: sizeof( ((struct name*)0)-
b ), another is your expression.
There exists a special rule in C99:
"If the operand (of the unary & operator) is the result of a unary *
operator, neither that operator nor the & operator is evaluated and
the result is as if both were omitted"
The expression &(((struct name*)0)->b) can be re-written as
&((*((struct name*)0)).b) and thus the above rule can be applied -
neither the & nor the * operator will be evaluated. No evaluation then
means no undefined behavior.

The operand is not the result of a unary *, it is the result of
selecting an element of a struct *after* dereferencing the unary * to
find the relevant instance of the struct. Therefore that exception does
not apply.

There is also still the question "why not use the offsetof macro? that's
what it's there fore."
--
Flash Gordon
Yes, you are right - the operand is not the result of the unary *
operator and thus my cited rule can not be applied.

As other posters have already said: the expression (unsigned
int)&(((struct name*)0)->b) is undefined since dereferencing a null
pointer. For the reason, we should not write suchlike code. Instead we
should use the offsetof macro defined in <stddef.hdirectly.

Thanks.

Apr 26 '07 #15
What the hell are you trying to do?

Nothing, This is not the code I contrived by myself!
I saw this code in a C Quiz site.
I suspected it to be a UB and at the same time I was not sure if it's
really one.
So I just want to get it confirmed before proclaiming to myself that
it is a UB!

I thank you all for your responses. Finally, my doubt is clear and
now, I am sure that it is a UB.
Apr 26 '07 #16
On Apr 24, 5:54 pm, Rajesh S R <SRRajesh1...@gmail.comwrote:
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.
I thank all of you for your responses. Now a new question has popped
out of my mind. Let me clarify that I am very much aware that my
present question is not based on standards. Therfore one can not use
standards to give a reply. This question is about the implementation
and optimisation.

My question is:
Can the expression (unsigned int)&(((struct name*)0)->b) be evaluated
at translation time itself, so that no ru time error is produced?

The thing that prompted me to pose this question is that this program
runs successfully without causing any segmentation faults in DEV-CPP
compiler for Win-32. This makes me think that the above expression is
evaluated at translation time itself. Please clarify.

Apr 27 '07 #17
In article <11**********************@r35g2000prh.googlegroups .com>,
Rajesh S R <SR**********@gmail.comwrote:
>My question is:
Can the expression (unsigned int)&(((struct name*)0)->b) be evaluated
at translation time itself, so that no ru time error is produced?
*If* the expression were legal C, then one would expect it to be a
constant expression, just as &((&foo)->b) is when foo is a static struct
name. It would therefore be natural for the compiler to evaluate it
at compile time.

It's not legal C, but it would still be natural for a compiler that
accepted it to evaluate it at compile time.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Apr 27 '07 #18
Rajesh S R wrote:
>
.... snip ...
>
My question is:
Can the expression (unsigned int)&(((struct name*)0)->b) be evaluated
at translation time itself, so that no ru time error is produced?
That conversion is implementation defined, so there is no reason to
assume its operation is the same, or even similar, on another
compiler or machine.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

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

Apr 27 '07 #19
Rajesh S R wrote, On 27/04/07 16:40:

<snip>
out of my mind. Let me clarify that I am very much aware that my
present question is not based on standards. Therfore one can not use
standards to give a reply. This question is about the implementation
and optimisation.

My question is:
Can the expression (unsigned int)&(((struct name*)0)->b) be evaluated
at translation time itself, so that no ru time error is produced?
As it is undefined behaviour the compiler can treat it as a run time
error, compile time error, or anything else it wants. It does not even
have to treat it the same each time it sees it.
The thing that prompted me to pose this question is that this program
runs successfully without causing any segmentation faults in DEV-CPP
compiler for Win-32. This makes me think that the above expression is
evaluated at translation time itself. Please clarify.
It is evaluated or not whenever the compiler or compiled program happens
to do it.
--
Flash Gordon
Apr 27 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: KidLogik | last post by:
Hello! I am converting an infix expression string into a postfix so that I will be able to evaluate it easier -> (5*(((9+8)*(4*6))+7)) == 598+46**7+* I believe the rule is "Replace all...
6
by: Sergey | last post by:
Hello! Could anybody be kind enough to explain this concept? Why C++ make two ops for prefix and postfix ++ operator? As I guess, it is possible to implement both cases via sole prefix...
8
by: lovecreatesbeauty | last post by:
Hello experts, Why can this difference between prefix increment/decrement and postfix increment/decrement reside in built-in operators for built-in data types? Thanks. // test.cpp // //...
13
by: Oleg Kharitonov | last post by:
Hello! I have found an interesting thing with postfix operator ++. What should contain the variable i after exceution the following code: int i = 5; i = i++; In VC++ 7.1 and VC++ 2005...
19
by: caramel | last post by:
i've been working on this program forever! now i'm stuck and going insane because i keep getting a syntax error msg and i just can't see what the compiler is signaling to! #include <stdio.h>...
30
by: Xah Lee | last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully Functional Notations Xah Lee, 2006-03-15 In LISP languages, they use a notation like “(+ 1 2)” to mean “1+2”....
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
2
by: swapnaoe | last post by:
Hi, In http://elearning.embnet.org/file.php/43/ctutorial/Postfix-and-prefix----and---.html I read about postfix and prefix unary operators. It said " If the increment or decrement operator is...
3
by: news.aioe.org | last post by:
Is it possible to overload increment(++) and decrement(--) postfix and prefix operators for primitive datatypes such as int, char, short, etc. in global scope (vs as a class member function where...
2
by: puzzlecracker | last post by:
How can we implement operator ++ as postfix in prefix? thanks
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.