1. 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, " "

2. Originally Posted by epidemic
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'

3. 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):
Code:
`for (p=c.begin(); p != c.end(); p++) {}`
and
Code:
`for (p=c.end(); p-- != c.begin();) {}`
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:

In the second loop above, in the last test, p is set to c.begin() - 1 which is invalid. It turns out that
Code:
`for (p=c.end()-1; p >= c.begin(); p--) {}`
is invalid for the same reason. Apparently the only correct way to do it is
Code:
```for (p=c.end(); p != c.begin();) {
p--;
// rest of loop
}```
Although my second loop with the integer variable worked properly, I could have written it similarly as
Code:
```for (int i=n; i != 0;) {
i--;
// 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.

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?

4. I would write your loops as:
Code:
`for (Iterator p = c.begin(); p != c.end(); ++p) {}`
and
Code:
`for (ReverseIterator p = c.rbegin(); p != c.rend(); ++p) {}`
In the second loop above, in the last test, p is set to c.begin() - 1 which is invalid.
That is true.

It turns out that
Code:
`for (p=c.end()-1; p >= c.begin(); p--) {}`
is invalid for the same reason.
I disagree. For random access iterators:
Code:
`p = c.end() - 1;`
must have the same net effect with respect to p as:
Code:
```p = c.end();
p--;```
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 is
past-the-end". As far as I can tell, the code snippets above satisfy the pre-conditions.

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 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.

5. What I was referring to was not the validity of
Code:
`p = c.end() - 1;`
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.

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.