473,625 Members | 3,222 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A popping question

mdh
Could someone help me understand one aspect of an exercise in K&R II;
(page 76)

Given function:

double pop( void){

....return a double from a stack if present

}

how does this work?

/* adds 2 doubles*/

case '+' :

push ( pop() + pop()) ;

I would have thought you would have to do something like

double firstpop, secondpop;

firstpop=pop();
secondpop=pop() ;
push ( firstpop+second pop);

ie where do the doubles go if they are not assigned. It obviously goes
somewhere and it does work, it's just that I do not see how.

Thanks in advance.

Sep 21 '06 #1
20 1607
"mdh" <md**@comcast.n etwrites:
how does this work?
....
push ( pop() + pop()) ;

I would have thought you would have to do something like

double firstpop, secondpop;

firstpop=pop();
secondpop=pop() ;
push ( firstpop+second pop);

ie where do the doubles go if they are not assigned. It obviously goes
somewhere and it does work, it's just that I do not see how.
It doesn't matter. It's the compiler's job to reserve space for
temporaries used in expressions. The programmer doesn't have to
worry about it.
--
"Programmer s have the right to be ignorant of many details of your code
and still make reasonable changes."
--Kernighan and Plauger, _Software Tools_
Sep 21 '06 #2
mdh

"mdh" <md**@comcast.n etwrites:

ie where do the doubles go if they are not assigned. It obviously goes
somewhere and it does work, it's just that I do not see how.
Ben Pfaff wrote:
It doesn't matter. It's the compiler's job to reserve space for
temporaries used in expressions. The programmer doesn't have to
worry about it.

So is it fair to assume that "pop" is in a sense replaced by it's
value, and any assignment is "additonal" but not necessary to the
execution of "pop"?

Sep 21 '06 #3
"mdh" <md**@comcast.n etwrites:
>"mdh" <md**@comcast.n etwrites:
ie where do the doubles go if they are not assigned. It obviously goes
somewhere and it does work, it's just that I do not see how.

Ben Pfaff wrote:
>It doesn't matter. It's the compiler's job to reserve space for
temporaries used in expressions. The programmer doesn't have to
worry about it.

So is it fair to assume that "pop" is in a sense replaced by it's
value, and any assignment is "additonal" but not necessary to the
execution of "pop"?
Do you have an idea that the result of a function must be
immediately assigned to a variable, or that otherwise it is lost?
That is how I interpret your words. This idea is wrong. All of
the following are allowed in C, given an appropriate function foo
and variable x:
foo(); /* Throws away return value. */
x = foo(); /* Assigns return value. */
x = foo() + 1; /* Assigns return value plus 1. */
x = foo() + foo(); /* Calls foo twice, assigns the sum. */
--
int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Sep 21 '06 #4
mdh

Ben Pfaff wrote:
Do you have an idea that the result of a function must be
immediately assigned to a variable, or that otherwise it is lost?
That is how I interpret your words. This idea is wrong.

no....I did not have an fixed idea...although it may have come across
that way...but your example clarifies it for me.

thanks.

Sep 21 '06 #5
Ben Pfaff wrote:
"mdh" <md**@comcast.n etwrites:
>how does this work?
...
>push ( pop() + pop()) ;

I would have thought you would have to do something like

double firstpop, secondpop;

firstpop=pop() ;
secondpop=pop( );
push ( firstpop+second pop);

ie where do the doubles go if they are not assigned. It obviously goes
somewhere and it does work, it's just that I do not see how.

It doesn't matter. It's the compiler's job to reserve space for
temporaries used in expressions. The programmer doesn't have to
worry about it.
Fine for + and *. Change it to - or / and things are different.

--
Some informative links:
news:news.annou nce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

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

Sep 21 '06 #6
CBFalconer <cb********@yah oo.comwrites:
Ben Pfaff wrote:
>"mdh" <md**@comcast.n etwrites:
>>how does this work?
...
>>push ( pop() + pop()) ;

I would have thought you would have to do something like

double firstpop, secondpop;

firstpop=pop( );
secondpop=pop ();
push ( firstpop+second pop);

ie where do the doubles go if they are not assigned. It obviously goes
somewhere and it does work, it's just that I do not see how.

It doesn't matter. It's the compiler's job to reserve space for
temporaries used in expressions. The programmer doesn't have to
worry about it.

Fine for + and *. Change it to - or / and things are different.
The OP's question was about memory. You are talking about order
of evaluation. The K&R2 page in question mentions that - and /
will not (reliably) do what we want for order of evaluation, so
there was no reason for me or the OP to bring it up.
--
Just another C hacker.
Sep 21 '06 #7
mdh

Ben Pfaff wrote:
>
The OP's question was about memory. You are talking about order
of evaluation. The K&R2 page in question mentions that - and /
will not (reliably) do what we want for order of evaluation, so
there was no reason for me or the OP to bring it up.

Hi Ben,
Correct....I probably did think that any value had to be assigned hence
the question. I think, even in C, one has to take certain things as
"given" as your example clarified. And yes...I am aware of the order of
things.

Sep 21 '06 #8
On 21 Sep 2006 15:36:21 -0700, "mdh" <md**@comcast.n etwrote:
>
Ben Pfaff wrote:
>>
The OP's question was about memory. You are talking about order
of evaluation. The K&R2 page in question mentions that - and /
will not (reliably) do what we want for order of evaluation, so
there was no reason for me or the OP to bring it up.


Hi Ben,
Correct....I probably did think that any value had to be assigned hence
the question. I think, even in C, one has to take certain things as
"given" as your example clarified. And yes...I am aware of the order of
things.
I hope you meant to say "the lack of order".
Remove del for email
Sep 22 '06 #9
mdh
Ben Pfaff wrote:
>
The OP's question was about memory. You are talking about order
of evaluation. The K&R2 page in question mentions that - and /
will not (reliably) do what we want for order of evaluation, so
there was no reason for me or the OP to bring it up.

Hi Ben,
Correct....I probably did think that any value had to be assigned hence
the question. I think, even in C, one has to take certain things as
"given" as your example clarified. And yes...I am aware of the order of
things.

Barry Schwarz wrote:
I hope you meant to say "the lack of order".
Clearly carrying some deeper meaning, deeper than my knowledge. Care
to explain?

Sep 22 '06 #10

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

Similar topics

2
2413
by: Obscurr | last post by:
hi! I was wondering how I could send a mail without the mail client popping up to the user. I would like a mail to be sent to myself when someone's pushing a button on my homepage. Any ideas? obscurr
17
3860
by: Paul Rubin | last post by:
Dumb question from a Windows ignoramus: I find myself needing to write a Python app (call it myapp.py) that uses tkinter, which as it happens has to be used under (ugh) Windows. That's Windows XP if it makes any difference. I put a shortcut to myapp.py on the desktop and it shows up as a little green snake icon, which is really cool and Pythonic. When I double click the icon, the app launches just fine and the tkinter interface does...
4
4836
by: ~Gee | last post by:
Hi, When I try to compile the following program, I get the following error: $ g++ anotherTest.C anotherTest.C: In function `int main()': anotherTest.C:47: void value not ignored as it ought to be What I am trying to do is that I have a global queue. I create an object of class "testClass" and then add it to the queue. My question
3
1443
by: Lee David | last post by:
I'm trying to have a popup window of new changes when a person comes to my web page and something has changed. I can see the "alert" popping up, but not the page. The page would look better and wouldn't stop the original page from opening until it was closed. Here is my code: <body bgcolor="lightgoldenrodyellow" onload="initindex();"> <div id="flyingimage1" style="position:absolute; left:0px; top:150px; z-index:0;"> <img...
3
5683
by: Bill Nguyen | last post by:
Once applied the latest security patches from Microsoft, OL on our VB app client machines keep popping up dialog box asking for permission to send mail using OL. Is there anyway to by pass this programmatically or by configuring OL client? Thanks a million. Bill
5
3500
by: Russell Warren | last post by:
Does anyone have an easier/faster/better way of popping from the middle of a deque than this? class mydeque(deque): def popmiddle(self, pos): self.rotate(-pos) ret = self.popleft() self.rotate(pos) return ret
3
2756
by: Learner | last post by:
Hello, I have two buttons on one of my VehicleDetails.aspx page. Obiviously these two buttons takes the user to two different pages. Now my client is interested in having a linkbutton instead of the two buttons. Once the user clicks on the linkbutton a javascript dialog box popsup that says "Is the Lessee buying this vehicle?" and with two buttons "Yes" and "No". If user clicks "yes" it should go to DealerQuestions.aspx" and if "No" it...
0
5456
by: ray007x | last post by:
I have a question for Beth Melton. Hello. This is Ray. I read an answer you graciously gave to another user about the constant popping up of "Windows Installer - Preparing to install". I cannot find anything to eliminate this headache and would be grateful for your insight and suggestions. Many people suggest running something like Windows Install Clearner (not sure that's the correct name), but on the ms website about it, we are...
20
3735
by: cowboyrocks2009 | last post by:
Hi, I need help to automate my code to take data from input file. Also I need to create it as a function so that I can pass it to some other program. I am new to Java so having a bit limitation to do this. My tab delimited Input File looks like this:- 21 p 13e 0 62 1 580001 andrew -14.53 -13.95 0 0 21 p 13d 63 124 580002 1160001 andrew -13.95 -13.37 0 0 21 p 12g 311 364 2900000 3385714 john -11.63 -11.14 0 0 21 q 11.1a 1274 1321...
0
8259
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8192
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,...
0
8696
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, 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...
0
8637
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8358
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7188
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, and deployment—without 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...
0
4090
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...
1
1805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1504
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.