473,467 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

plz explain this TC++ output

4 New Member
this is a code which i met by. The question is to find the output. but the one i got by myself had little resemblence with that given by the computer. i asked my teacher my friends. but none of them could give a sensible reason. i reckon there could be a wierd rearrangement or precedence rules in Turbo C++ which i am not aware of.

plz give it a try. and dont forget to post some replies.

the code is:
void main()
{ int x[]= {10,20,30,40};
int *p,*t,**q;
p=x;
t=x+1;
q=&t;
cout <<*p<<'\t'<<**q<<'\t'<<*t++;
}





so got 10 20 20 eh?

but sorry my dear friend, the computer gives me
10 30 20

thanx for posting a reply!!
Mar 31 '07 #1
4 1241
Savage
1,764 Recognized Expert Top Contributor
this is a code which i met by. The question is to find the output. but the one i got by myself had little resemblence with that given by the computer. i asked my teacher my friends. but none of them could give a sensible reason. i reckon there could be a wierd rearrangement or precedence rules in Turbo C++ which i am not aware of.

plz give it a try. and dont forget to post some replies.

the code is:
void main()
{ int x[]= {10,20,30,40};
int *p,*t,**q;
p=x;
t=x+1;
q=&t;
cout <<*p<<'\t'<<**q<<'\t'<<*t++;
}





so got 10 20 20 eh?

but sorry my dear friend, the computer gives me
10 30 20

thanx for posting a reply!!
You havent assigned pointers well.You should use:

Expand|Select|Wrap|Line Numbers
  1. *p=x;
  2. *t=x+1;
Mar 31 '07 #2
JosAH
11,448 Recognized Expert MVP
You too hit the 'undefined behaviour' behaviour when you alter the value of an
alterable lvalue more than once or both dereference and alter it before a
sequence point has been reached.

In the middle of an expression there are no sequence points and your code alters
the value of 't' while dereferencing it for lvalue 't'

C and C++ are not Java: they can evaluate any part of an expression in any
order they want as long as it follows the semantics of a defined abstract
machine (as per the ANSI/ISO standard). Java's defined behaviour is that
it has no undefined behaviour and always evaluates an expression from left
to right respecting operator precedence.

Your code simply violated that C and C++ constraint so nothing more can be
said about any behaviour on any platform whatsoever.

kind regards,

Jos
Mar 31 '07 #3
prannoy
4 New Member
You havent assigned pointers well.You should use:

Expand|Select|Wrap|Line Numbers
  1. *p=x;
  2. *t=x+1;
actually i hav tested the code on tc++ compiler. and i got 10 30 20 without errors. anyway thanx for your reply.
Apr 1 '07 #4
prannoy
4 New Member
You too hit the 'undefined behaviour' behaviour when you alter the value of an
alterable lvalue more than once or both dereference and alter it before a
sequence point has been reached.

In the middle of an expression there are no sequence points and your code alters
the value of 't' while dereferencing it for lvalue 't'

C and C++ are not Java: they can evaluate any part of an expression in any
order they want as long as it follows the semantics of a defined abstract
machine (as per the ANSI/ISO standard). Java's defined behaviour is that
it has no undefined behaviour and always evaluates an expression from left
to right respecting operator precedence.

Your code simply violated that C and C++ constraint so nothing more can be
said about any behaviour on any platform whatsoever.

kind regards,

Jos

THANK YOU JOS!!!!!!!!!
i think ur reply most matches my problem. i really thank you for answering the 'unanswerable' question of mine. kudos to you!!

regards,

Prannoy
Apr 1 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Bartosz Wegrzyn | last post by:
I wrote this script for my QOS traffic. I only care about my VOIP service. Rest will fight for the bandwidth. my script: #!/bin/bash echo 1 > /proc/sys/net/ipv4/ip_forward
14
by: Ina Schmitz | last post by:
Hello all, I don't succeed in displaying the explain plan. I use IBM DB2 Universal Database 8.2. I tried to do the example given in the online help for "Visual Explain". The tables...
22
by: Jaspreet | last post by:
I was recently asked this question in an interview. Unfortunately I was not able to answer it and the interviewer made a decision on my C strengths (or weekness) based on this single question and...
1
by: Terrance | last post by:
Hello, I was wondering if someone can help me understand something. Below is some code that I got from the MS website that is suppose to authenticate for the username and password on the local...
9
by: Casanova | last post by:
int main() { int i=5; printf("\n%d",++i + ++i + ++i + ++i + ++i ); } Why does the above program print 40 as output in TC and 41 in GCC? logically it appeals to me that 40 makes more sense...
10
by: honey | last post by:
I have downloaded c api that have many h file and c file and one main file. I use turbo c compiler. So I put header files under C:\tc\include and I put c files under C:\tc\bin I also declare...
3
by: Guofu Chen | last post by:
Hi, In TC++PL, Chapter 10, there is an exercise which ask us to modify the following program: #include <iostream> int main() { std::cout << "Hello, world!/n" ;
3
by: lenygold via DBMonster.com | last post by:
Total Cost: 43.712 Query Degree: 1 Rows RETURN ( 1) Cost I/O | 3.44
0
by: LanaR | last post by:
Hello, one sql statement is causing severe performance issue. The problem occurs only in UDB environment, the same statemnt on the mainframe is running fine. I have an explain output from the sql....
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
marktang
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,...
0
Oralloy
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,...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
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 project—planning, coding, testing,...
0
isladogs
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.