了解C++中的内存地址

aej*_*yun 1 c++

为什么地址不会随着我增加str而改变?我认为当我执行指针运算时,指针指向不同的内存地址.因此,内存地址不应该改变吗?

#include <iostream>
using namespace std; 

void reverse(char* str){
    cout << &str << endl;

    while(*str != '\0'){
        cout << &str << endl;
        str++; 
    }
}


int main(){

    char str[] = "hello"; 
    reverse(str); 

}
Run Code Online (Sandbox Code Playgroud)

Rya*_*anP 5

&str是指针的地址.在迭代字符时更改指针,但是您要更改的指针仍位于同一位置.

编辑:更改您cout << &str << endl;的内容cout << "pointer loc<" << &str << "> pointer value<" << (void*)str << ">" << endl;并查看其内容.