Wrox Programmer Forums
|
BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5
This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2008 by Ivor Horton; ISBN: 9780470225905
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 5th, 2009, 04:47 AM
Registered User
 
Join Date: Apr 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question Interpretation of the calculator "eatspaces" function

Hello,

Can someone please explain how the following term:

Code:
( *(str+i) = *(str+j++) )
evaluates when used in the following line

Code:
	while ( (*(str+i) = *(str+j++) )!= '\0' )
in the "eatspaces" function on page 302 of this book? Note that this is a function which is used in the Calculator Example, which is Ex6_09.

At first glance, I thought it was assigning the value of the pointer at *(str+j++) to *(str+i), and then checking that this wasn't the end of the line... but that doesn't seem to be quite correct. I say that's not quite correct because when I try to spread these commands over a few lines, it doesn't give the same result.

This means one of two things:
(1) My attempt to spread the code out over two lines is rubbish, with a bug in it, or
(2) My interpretation of this line of code is faulty.

Can someone please let me know which of the two problems it is?

Cheers,
- Steve.
 
Old April 5th, 2009, 03:44 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Don't have the book, but that code doesn't really make sense to me.

Since no place there does the value of i get changed, that sure looks to me like *ALL* the characters starting at j get copied into the SINGLE character at i, from position j to the end of the string.

Sure looks like a typo/error to me.

What are i and j initialized to??

I could see *THIS* code being used to "eat spaces":
Code:
int i = ... find a space in the str string...
int j = i;
while ( (*(str+i) = *(str+j++) ) == ' ' );
while ( (*(str+(++i)) = *(str+j++) ) != '\0' ' );
being used to remove spaces.

Incidentally, I personally find that notation distasteful. This notation is 100% equivalent (according to the laws of C and C++) and, I think, is clearer:
Code:
int i = ... find a space in the str string...
int j = i;
while ( ( str[i] = str[j++] ) == ' ' );
while ( ( str[++i] = str[j++] ) != '\0' );
 
Old May 20th, 2009, 02:09 AM
Authorized User
 
Join Date: May 2009
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
Default yup

yup yup yup





Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a files general property of "Type of file" GregSivers Visual Basic 2008 Essentials 7 June 3rd, 2009 09:38 AM
Code not going as planned: "icicle" vs "savedinstancestate" joopthecat BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 3 May 3rd, 2009 03:09 PM
Chapter-5 on Intents:ContactPickerTester does not show the button "Pick a Contact" sunilm12 BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 3 April 15th, 2009 11:55 AM
How to download Code in "Professional Active Server Page 3.0 " book. renjith0 All Other Wrox Books 2 April 2nd, 2009 05:06 AM
google Response.Write(Request.QueryString("q")) senol01 ASP.NET 2.0 Basics 1 January 2nd, 2009 12:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.