C/C++ 返回局部變量-error

還是舉一個例子,將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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章