you could ask the user to enter that number in a character array, and just do - 48.
a[0] = 1 , to get the number , do a[0] - 48
a[1] = 2 " "
a[2 ] = 3, " "
you could ask the user to enter that number in a character array, and just do - 48.
a[0] = 1 , to get the number , do a[0] - 48
a[1] = 2 " "
a[2 ] = 3, " "
Don't use magic constants... If a[0] == '1' to get a number use a[0] - '0'Originally Posted by epidemic
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
I spent some more time thinking about the "proper" way to write forwards and backwards loops. If one wants the loop variable to be local to the loop, this requires a for loop. If instead of an integer loop variable, one has an iterator for a container c, then my loops would be written (leaving out the definition for p):
andCode:for (p=c.begin(); p != c.end(); p++) {}
but as I've read recently, the standard says that the behavior is undefined if one merely _creates_ an invalid pointer, even if it's never dereferenced - for example see the _long_ thread here:Code:for (p=c.end(); p-- != c.begin();) {}
http://groups.google.com/group/comp....18f6cccd56427d
In the second loop above, in the last test, p is set to c.begin() - 1 which is invalid. It turns out that
is invalid for the same reason. Apparently the only correct way to do it isCode:for (p=c.end()-1; p >= c.begin(); p--) {}
Although my second loop with the integer variable worked properly, I could have written it similarly asCode:for (p=c.end(); p != c.begin();) { p--; // rest of loop }
which would be clearer although longer. If the loop was written in Java, the "!= 0" would have to be included in any case.Code:for (int i=n; i != 0;) { i--; // rest of loop }
One thing confuses me about the creation of invalid pointers. An uninitialized local variable can normally take on any possible value. Does this mean that a local pointer variable HAS to be initialized immediately to avoid having an invalid value? Or is it only if one explicitly assigns an invalid value to it that one has a problem?
I would write your loops as:
andCode:for (Iterator p = c.begin(); p != c.end(); ++p) {}
Code:for (ReverseIterator p = c.rbegin(); p != c.rend(); ++p) {}That is true.In the second loop above, in the last test, p is set to c.begin() - 1 which is invalid.
I disagree. For random access iterators:It turns out that
is invalid for the same reason.Code:for (p=c.end()-1; p >= c.begin(); p--) {}
must have the same net effect with respect to p as:Code:p = c.end() - 1;
So either they are both invalid, or they are both valid. In my opinion, they are both valid. The C++ standard states that the pre-condition for --r (and thus r--) for some iterator r is that "there exists s such that r == ++s". The post-condition is that "s is dereferenceable". But the pre-condition for ++r is "r is dereferenceable" with the post-condition that "r is dereferenceable or r isCode:p = c.end(); p--;
past-the-end". As far as I can tell, the code snippets above satisfy the pre-conditions.
I think that there is no problem since it is uninitialised. Of course, it is good practice to initialise a pointer to null in such a case.An uninitialized local variable can normally take on any possible value. Does this mean that a local pointer variable HAS to be initialized immediately to avoid having an invalid value? Or is it only if one explicitly assigns an invalid value to it that one has a problem?
Last edited by laserlight; 01-03-2007 at 09:17 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
What I was referring to was not the validity of
although there actually is another issue there - if c has zero length, then c.end() == c.begin() so even the initial value is invalid. I was thinking of the fact that in the last iteration of the loop, p == c.begin(), and then at the end of the loop, p-- tries to set p to c.begin() - 1. The loop assumes that the next test will fail, but if the code is running on something that checks each pointer assignment and traps immediately if the value is invalid, the program may crash right there. Or there may be some kind of wraparound so the test doesn't fail. The point is, as soon as the invalid pointer is generated, all bets are off.Code:p = c.end() - 1;
Edit: Please replace the word "pointer" with "iterator" in the above - I'm much more familiar with pointers than general iterators, although since pointers are a special case, the issues are the same.
Last edited by robatino; 01-04-2007 at 12:18 AM.