I have a simple code:
void function1(int* A);
void function2(int* A);
int main(int argc, char* argv[]) {
int* A = new int[4];
// Readdressed into function1: A[0] is preserved
A[0] = 21;
function1(A);
cout << "\nA[0] is: " << A[0];
// Result A[0] = 21
// Is not readdressed into function2: A[0] is not preserved
A[0] = 21;
function2(A);
cout << "\nA[0] is: " << A[0];
// Result A[0] = 23
return 0;
}
void function1(int* A) {
A = new int[4];
A[0] = 23;
}
void function2(int* A) {
A[0] = 23;
}
Output:
In the case of function1 output A[0] is 21
In the case of function2 output A[0] is 23
Question: Why does A-pointer not get (In the first case) an address to a new allocated memory cells where A[0] is 23, and preserve an address where A[0] is 21 instead ?