aejhyun
aejhyun

Reputation: 612

Understanding Memory Addresses in C++

Why doesn't the address change as I increment str? I thought that when I performed pointer arithmetic, the pointer points to a different memory address. Therefore, shouldn't the memory address change as well?

#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); 

}

Upvotes: 1

Views: 131

Answers (1)

RyanP
RyanP

Reputation: 1918

&str is the address of the pointer. You are changing the pointer as you iterate over the characters, but the pointer that you are changing is still located at the same spot.

Edit: change your cout << &str << endl; to cout << "pointer loc<" << &str << "> pointer value<" << (void*)str << ">" << endl; and see what it says.

Upvotes: 5

Related Questions