Mấy anh cho em hỏi tại sao hai đoạn code dưới đây lại ra đáp án khác nhau vậy ạ (giữa int* &ptr với int* ptr)
Đoạn code này sẽ in ra NULL:
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
void setToNull(int* &ptr)
{
ptr = NULL;
}
int main()
{
int value = 5;
int* pValue = &value;
cout << "pValue point to " << pValue << endl;
setToNull(pValue);
if (pValue == NULL)
cout << "pValue point to NULL" << endl;
else
cout << "pValue point to " << pValue << endl;
return 0;
}
Đoạn code này không in ra NULL:
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
void setToNull(int* ptr)
{
ptr = NULL;
}
int main()
{
int value = 5;
int* pValue = &value;
cout << "pValue point to " << pValue << endl;
setToNull(pValue);
if (pValue == NULL)
cout << "pValue point to NULL" << endl;
else
cout << "pValue point to " << pValue << endl;
return 0;
}