我正在學習C++中的指針和引用變量,並且我看到了一個示例代碼。我不確定爲什麼* c的值從33變爲22.有人能幫我理解這個過程嗎?爲什麼當我沒有給它賦值時變量的值會改變?
int a = 22;
int b = 33;
int* c = &a; //c is an int pointer pointing to the address of the variable 'a'
int& d = b; //d is a reference variable referring to the value of b, which is 33.
c = &b; //c, which is an int pointer and stored the address of 'a' now is assigned address of 'b'
std::cout << "*c=" << *c << ", d=" << d << std::endl; //*c= 33 d= 33
d = a; //d is a reference variable, so it cannot be reassigned ?
std::cout << "*c=" << *c << ", d=" << d << std::endl; //*c= 33 d= 33
非常感謝! – Skipher
@Skipher,不客氣。 –