Char pointers vs Int pointers passed to functions

2019-02-27 10:38发布

问题:

I thought I understood pointers, but I think there's a nuance by how they're treated differently that I'm not quite following. When I pass an integer pointer or the address of an integer to showInt, it will print out the same memory address as it would outside the function. However. When I pass the following pointer to showChar;

char* value = "One";
showChar(value);

The address of the first element is different inside the function than it is outside the function. I understand that this is behaviour consistent with passing by value, and that a copy of the pointer is made within the function, however I was under the impression that a copy of a pointer still held the same address. Why is it different when dealing with pointers to char? If the char pointer just stores the address of the first element of the string literal, then why would the pointer in the function not point to the same memory location, but instead point to a new area in memory? That suggests to me that it isn't copying a char pointer, but creating a new char pointer and assigning it the value pointed to by the original pointer. If so, I don't understand why.

I understand that you can access the pointer address in the function by passing a pointer-to-pointer, or a reference-to-pointer instead, but why this is the case still confuses me.

Passing a pointer to char;

void showChar(char* name){

 cout << name << endl;
 cout << &name << endl;
}

Passing a pointer to int;

void showInt(int* num){

 cout << num << endl;
 cout << *num << endl;
}

回答1:

Your showChar and showInt functions are printing different things.

In showChar, this:

cout << &name << endl;

prints the address of name, which is a local variable. In showInt, you don't print the value of &num; rather you print the value of num, which is an address, but not the address of a local variable.

In showChar, if you want to print value of name as an address, you'll need to convert it to some other pointer type such as void*:

cout << (void*)name << endl;

because the overload of operator<< for char* dereferences the char* pointer and prints the C-style string that it points to.

In more detail:

void showChar(char* name){
 cout << name << endl;    // prints the contents of the string that
                          // `name` points to
 cout << &name << endl;   // prints the address of the local variable `name`
}

void showInt(int* num){
 cout << num << endl;     // prints the value of the pointer `num`
 cout << *num << endl;    // prints the value of the `int` object that
                          // `num` points to
}


回答2:

The first line in showChar:

    cout << name << endl;

will print the contents behind the pointer name, since the IO streams operators in C++ are overloaded to print char-pointers as text instead of the pointer as a number.

With the second line in this function

    cout << &name << endl;

you don't print the pointer address name but instead the pointer (address) of the local variable name (which happens to be of a pointer type). That's taking the reference one time too much. What you want is change the behavior of how operator<<(ostream &, char*) works, namely printing the pointer address instead of printing a C-string.

You can do this by simply converting the pointer before passing it to cout, like this:

void showChar(char* name){
    cout << (void*)name << endl;
}