void print(int *a,int *b,int *c,int *d,int *e){
printf("++ptr=%d\nptr--=%d\nptr=%d\nptr++=%d\n++ptr=%d
\n",*a,*b,*c,*d,*e);
}
int main(void){
static int arr[]={97,98,99,100,101,102,103,104};
int *ptr=arr+1;
print(++ptr,ptr--,ptr,ptr++,++ptr);
return 0;
}
gives me ::
++ptr=100
ptr--=100
ptr=100
ptr++=99
++ptr=100
I am totally foxed by this answer. Please try to explain why the
answer must be like that ?? 3 1146
onkar wrote:
[...]
print(++ptr,ptr--,ptr,ptr++,++ptr);
[...]
I am totally foxed by this answer. Please try to explain why the
answer must be like that ??
For the exact same reasons given in your "tricky output" thread from
last week. You have invoked undefined behavior by modifying ptr
more than once between sequence points.
(And note that there is no "comma operator" in the above statement,
which would have been a sequence point.)
And note that the output doesn't "must be" like that. It happens to
be that way on your particular compiler, with the particular set of
options you compiled it. With UB, _anything_ could happen.
My compiler, with default settings, gives:
++ptr=100
ptr--=99
ptr=99
ptr++=99
++ptr=99
With optimization on, it gives:
++ptr=100
ptr--=99
ptr=100
ptr++=99
++ptr=100
Note that _both_ sets of output are "correct" as far as the Standard
is concerned.
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
onkar <onkar....@gmail.comwrote:
void print(int *a,int *b,int *c,int *d,int *e){
* * * * printf("++ptr=%d\nptr--=%d\nptr=%d\nptr++=%d
\n++ptr=%d\n",*a,*b,*c,*d,*e);}
int main(void){
* * * * static int arr[]={97,98,99,100,101,102,103,104};
* * * * int *ptr=arr+1;
* * * * print(++ptr,ptr--,ptr,ptr++,++ptr);
* * * * return 0;
}
gives me ::
++ptr=100
ptr--=100
ptr=100
ptr++=99
++ptr=100
I am totally foxed by this answer.
Why do you expect to receive a different answer to
same question you asked a few days ago?
Tell us what aspects of previous answers (e.g. the FAQ)
are still causing confusion.
--
Peter
onkar wrote:
<snip the usual>
You *are* a troll, aren't you? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mark Wilson CPU |
last post by:
This must be easy, but I'm missing something...
I want to execute a Perl script, and capture ALL its output into a PHP
variable. Here are my 2 files:
-------------------------------------...
|
by: edgekaos |
last post by:
Is method 2 valid?
Method 1:
wstring input = L"STRING";
wstring output = input;
transform(output.begin(), output.end(), output.begin(), towupper);
Method 2:
wstring input = L"STRING";...
|
by: Kevin Mansel via .NET 247 |
last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
|
by: kalamantina |
last post by:
#include "stdafx.h"
#include <stdio.h>
#define output( x ) printf( #x "\r\n" );fflush( stdout )
class CMyBase
{
public:
CMyBase()
{
output( CMyBase() );
f(*this);
|
by: newbie |
last post by:
i'm a newbie of c language.
can anyone help me to implement the code so that I can get the
ciphertext from the output.
thanks.
#ifndef _3DES_H
#define _3DES_H
#ifndef uint8
#define uint8 ...
|
by: spibou |
last post by:
Is the output of the C preprocessor deterministic ? What I mean
by that is , given 2 compilers which conform to the same standard,
will their preprocessors produce identical output given as input...
|
by: MatsL |
last post by:
Hi,
This is seriously driving me crazy, could anyone explain to me why
neither of these doesn't produce XHTML compliant output (it is being
called in Render() btw):
output.WriteLine("<img...
|
by: undshan |
last post by:
I am writing a code that needs to open a file, create an output file, run through my function, prints the results to the output file, and closes them within a loop. Here is my code:
#include...
|
by: gabosom |
last post by:
Hi! I've been breaking my head trying to get the output variables from my Stored Procedure.
This is my SP code
CREATE PROCEDURE GetKitchenOrderDetail(
@idService int,
--outPut Variables ...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |