atoi() method, char * cout

2019-08-06 05:04发布

问题:

This is an atoi() I am trying to understand. In order to compile with the non-existing library, I called it m().

There are several lines of codes I am confused about, mainly char * issues.

My questions are listed after the code:

#include "stdafx.h"
#include <iostream>

using namespace std;

int m( char* pStr ) {   
    int iRetVal = 0;   
    int iTens = 1;   
    cout << "* pStr: " << * pStr << endl;   // line 1
    if ( pStr )  {    
        char* pCur = pStr;  
        cout << "* pCur: " << * pCur << endl;
        while (*pCur)  {     
            //cout << "* pCur: " << * pCur << endl; //line 2
            pCur++;   }
        cout << "pCur: " << pCur << endl;       //line 3
        cout << "* pCur: " << * pCur << endl;   //line 4
        pCur--;     
        cout << "pCur: " << pCur << endl;       //line 5
        while ( pCur >= pStr && *pCur <= '9' && *pCur >= '0' )     {       
            iRetVal += ((*pCur - '0') * iTens);      
            pCur--;       
            iTens *= 10;     }  }   
    return iRetVal; } 


int main(int argc, char * argv[])
{
    int i = m("242");
    cout << i << endl;
    return 0;
}

Output:

* pStr: 2
* pCur: 2
pCur:
* pCur:
pCur: 2
242

Questions:

line 1: Why the cout is 2? * pStr was passed in as a pointer to char by 242, isn't supposed to be 242 instead?
line 2: I have to comment out this cout since it looks like it is in a infinite loop.What does while (*pCur) mean? And why do we need this loop?
line 3: Why it doesn't print out anything?
line 4: Why it doesn't print out anything?
line 5: Why does it print out 2 now after it got decremented?

回答1:

In Line 1, you would use pCur not *pCur to output the string. In the first form it is a pointer to a char, which is taken as a string that ends at the first "null byte" ('\0'). In the second form the memory address to which the pointer points is being dereferenced, so you are actually printing a single character.

In Line 2, the intent is to exit the loop at the end of the string. Conveniently, strings end at the first "null byte", which is represented by a 0, which evaluates as false. The pCur++ moves the pointer along the string until that 0 is found.

In Line 3, pCur is now pointing to the null byte, which essentially translates to an empty string.

In Line 4, *pCur is dereferencing a pointer address again—but the character is a non-printable character, '\0', so you don't see anything in your output.

In Line 5, you moved the pointer back one space—to point to the ones digit of "242", so you see 2 as an output.



回答2:

To understand why this happens, you need to understand how strings work in C++, well really, how arrays of characters work. A string is really just an array of characters, ending with a null character (the value 0, not the digit 0). We pass this string along by pointing to the first character in the array. When we wish to print the string, we simply print the character pointed to, increase the pointer, and continue with this, until we reach the null character.

line 1: You dereference the pointer, it is really a pointer to an array of chars. So the pointer points to the first of the char. It looks like this:

char 1: 2 <-- The pointer points to this  
char 2: 4  
char 3: 2  
char 4: \0 (null byte)

By prepending the pointer with * you retrieve the value of what it points to, which is the character 2.

line 2: As I mentioned for line 1, *ptr is really the value of the character pointed to, so the while (*ptr) will continue as long the character pointed to by ptr is not 0. By increasing ptr we increase the pointer and at some point reach the null byte.

char 1: 2
char 2: 4  
char 3: 2  
char 4: \0 (null byte) <-- After the loop, this is what we point at

line 3: This is not an infinite loop, you check the value of the char pointed at, and if it is not 0, you increase the pointer. This is essentially how you iterate the characters of a string. For each iteration you would print out the character pointed to, (as you commted out section does), and increase the pointer, until you reach the null chacter.

Since you have increased the pointer above till it reached the null character, it will be pointing at the null character after the loop as well. So when you print the ptr pointer you really perform the loop above, printing all characters until you reach a null pointer. But in your case you already point at the null character.

char 1: 2
char 2: 4  
char 3: 2  
char 4: \0 (null byte) <-- We are still point to the null character, 
                           so it is treated as an empty string

line 4: You try to print out the character pointed to by ptr, but this is the null character so nothing is printed.

char 1: 2
char 2: 4  
char 3: 2  
char 4: \0 (null byte) <-- We are still point to the null character so no 
                           printing is done.

line 5: You decrease the pointer on the line above, meaning that it points to the previous element in the array. So uou are now pointing to the last '2' character, so it gets printed.

char 1: 2
char 2: 4  
char 3: 2  <-- You decreased it by one, so now we are pointing at 2.
char 4: \0 (null byte)