还是举一个例子,将str = strTmp 掉,对比前后输出的变化。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void foo(char **str) {
char *strTmp[4] = {nullptr};
strTmp[0] = reinterpret_cast<char *>('a');
str = strTmp;
str[0] = reinterpret_cast<char *>('a');
}
int main() {
char *str[4] = {nullptr};
foo(str);
if (str[0] == nullptr) {
cout << "11" << endl;
} else {
cout << "22" << endl;
}
}
本文通过一个具体的C++代码示例,深入探讨了指针的赋值与修改操作,展示了str与strTmp指针之间的关系及作用域内的变化,对于理解C++中指针的复杂行为具有指导意义。
830

被折叠的 条评论
为什么被折叠?



