当我没有给变量赋值时,为什么变量的值会改变? 我正在学习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 inta=22; int b=33; int*c=&a//c是指向变量“a”地址的int指针 int&d=b//d是参考变量,指的是b的值,即33。 c=&b//c、 这是一个整数指针,存储了地址“a”,现在分配给地址“b” 标准::cout
那是个误会。该语句将当我没有给变量赋值时,为什么变量的值会改变? 我正在学习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 inta=22; int b=33; int*c=&a//c是指向变量“a”地址的int指针 int&d=b//d是参考变量,指的是b的值,即33。 c=&b//c、 这是一个整数指针,存储了地址“a”,现在分配给地址“b” 标准::cout,c++,pointers,reference,C++,Pointers,Reference,那是个误会。该语句将a(22)的值赋给d作为引用的变量(b)。它确实改变了d所引用的内容。因此,在执行该行之后,b的值为22 那是个误会。该语句将a(22)的值赋给d作为引用的变量(b)。它确实改变了d所引用的内容。因此,在执行该行之后,b的值是22。让我们一步一步地运行这段代码: int a = 22; int b = 33; 我们给a,b赋值。没什么好说的 int* c = &a; c持有a的地址*c是a的值,现在是22 int& d = b; d是a到b。从现在起,d
a
(22)的值赋给d
作为引用的变量(b
)。它确实改变了d
所引用的内容。因此,在执行该行之后,b
的值为22
那是个误会。该语句将
a
(22)的值赋给d
作为引用的变量(b
)。它确实改变了d
所引用的内容。因此,在执行该行之后,b
的值是22。让我们一步一步地运行这段代码:
int a = 22;
int b = 33;
我们给a,b赋值。没什么好说的
int* c = &a;
c持有a的地址*c是a的值,现在是22
int& d = b;
d是a到b。从现在起,d被视为b的别名。d的值也是b的值,即33
c = &b;
c现在拥有b的地址*c是b的值,现在是33
d = a;
我们将22(a值)赋值给d。因为d是b的别名,所以b现在也是22。因为c指向b,*c是b的值,现在是22。让我们一步一步地运行这段代码:
int a = 22;
int b = 33;
我们给a,b赋值。没什么好说的
int* c = &a;
c持有a的地址*c是a的值,现在是22
int& d = b;
d是a到b。从现在起,d被视为b的别名。d的值也是b的值,即33
c = &b;
c现在拥有b的地址*c是b的值,现在是33
d = a;
我们将22(a值)赋值给d。因为d是b的别名,所以b现在也是22。因为c指向b,*c是b的值,现在是22。非常感谢!非常感谢你!感谢您的一步一步的过程!感谢您的一步一步的过程!