Consider the following code:
int main()
{
struct name
{
long a;
int b;
long c;
}s={3,4,5},*p;
p=&s;
printf("%d",*(i nt *)((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. 19 2492
In article <11************ **********@n35g 2000prd.googleg roups.com>,
Rajesh S R <SR**********@g mail.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
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
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",*(i nt *)((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
In article <f0**********@m urdoch.hpl.hp.c om>,
Chris Dollin <ch**********@h p.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
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
"Rajesh S R" <SR**********@g mail.comwrote in message
news:11******** **************@ n35g2000prd.goo glegroups.com.. .
printf("%d",*(i nt *)((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
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!
In article <11************ **********@b40g 2000prd.googleg roups.com>,
Rajesh S R <SR**********@g mail.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
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
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?
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",*(i nt *)((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.
"Rajesh S R" <SR**********@g mail.comha scritto nel messaggio
news:11******** **************@ n35g2000prd.goo glegroups.com.. .
Consider the following code:
int main()
{
struct name
{
long a;
int b;
long c;
}s={3,4,5},*p;
p=&s;
printf("%d",*(i nt *)((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.c om/group/comp.lang.c/msg/f0************* *************** ********@tdi.cu .mi.it
return 0;
}
Is the expression (unsigned int)&(((struct name*)0)->b) valid as per
C99?
What the hell are you trying to do? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 occurances of two operands followed
by an operator by their infix equivalent"
|
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 increment operation. Correct me if I'm wrong,
Let's see trival example:
|
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
//
// >g++ test.cpp
// test.cpp: In function `int main()':
|
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 beta 2 the variable i contains 6.
|
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>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
| |
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”.
Likewise, they write “(if test this that)” to mean “if (test) {this}
else {that}”. LISP codes are all of the form “(a b c ...)”, where the
|
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
involved. The problem
is that comparisons between signed and unsigned values are machine-
dependent, because
they depend on the sizes of the various integer types. For example,
suppose that int is 16 bits
|
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 used as a prefix, the operation is performed before the function call. If the operator is used as a postfix, the operation is performed after the function call. "
So accordingly
int x=4;
printf("%d%d", x++,x++);
the output should be 4 4
|
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 this is possibe)?
If there is a site that lists all operators that can't be overloaded (such as
the member access dot operator), please post it.
thanks
|
by: puzzlecracker |
last post by:
How can we implement operator ++ as postfix in prefix?
thanks
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |