-1

i'm code beginner

#include <bits/stdc++.h>
using namespace std;

void init(int* arr){
    arr = new int[10];
    cout << arr << endl;
}

int main(int argc, char* argv[]){
    int *arr;
    init(arr);
    cout << arr << endl;
}

this is a simple code. My question is that why arr's address in init function and arr's address in main function is different?

My think is that i gave arr's address to init function and in init function, arr is assigned a new address through 'new int[10]'

So, arr's address in init function and in main will be same.

but this code doesn't work as i think.....

Can you tell me why?

Leftddr
  • 79
  • 1
  • 7
  • 2
    Does this answer your question? [Is passing pointer argument, pass by value in C++?](https://stackoverflow.com/questions/4426474/is-passing-pointer-argument-pass-by-value-in-c) – 273K Mar 29 '21 at 04:51
  • You did _not_ give _"arr's address"_. `arr` is a pointer, and you passed its value. If you want to pass its _address_, you should use `&arr` and make the function accept a `int** arr` parameter. But that is very C-like, and you might consider using references instead because you are using C++, not C. – paddy Mar 29 '21 at 04:51
  • See https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h and https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice. As for the question at hand, you need a pointer to a pointer (though pass by reference would seem like a better choice in this case), so you're checking two entirely different variables. Also, you don't `delete` allocated memory from `arr`, which isn't too much of a problem in a test program like this but it's always good practice to remember. – mediocrevegetable1 Mar 29 '21 at 04:57

1 Answers1

3

Step back a little and think about how arguments are passed into functions.

void foo(int x) {
    
}

int main() {
    int x;
    foo(x);
}

Here the x in main() is actually passed by value to foo() which means a copy of x is created when passing to foo().

With the same logic, if you think of int * as another variable type

using intPtr = int*; //a sort of typedef

void foo(intPtr x) {
    
}

int main() {
    intPtr x;
    foo(x);
}

a copy of x is again created. This is what is happening in your program. For what you expect, you need to pass in the variable by reference

using intPtr = int*;

void foo(intPtr& x) {
    
}

int main() {
    intPtr x;
    foo(x);
}

Adapting the same to your program:

#include <iostream>

void init(int*& arr){
    arr = new int[10];
    std::cout << arr << '\n';
}

int main(int argc, char* argv[]){
    int *arr;
    init(arr);
    std::cout << arr << '\n';
    delete(arr);
}

There's another way with pointers that is using a pointer to a pointer (**)

#include <iostream>

void init(int** parr){
    *parr = new int[10];
    std::cout << *parr << '\n';
}

int main(int argc, char* argv[]){
    int *arr;
    init(&arr);
    std::cout << arr << '\n';
    delete(arr);
}

where the address of arr (&arr) is passed into the function. Inside the function, the contents of parr is modified (which is arr in this case).

Zoso
  • 3,273
  • 1
  • 16
  • 27