+ 5
[Solved] Dynamic arrays in C++
I was trying to read input with a function and assign that to a dynamic array, but unfortunately it doesn’t seem to work and I don’t know why… Please take a look at my code: https://code.sololearn.com/c9PxmQMlG2ge/?ref=app
8 Antworten
+ 4
Maisu You have to pass a pointer (or a reference) to a function for change it inside a function thought its a pointer.
You want change dynarr (int*) from read function with args arr (int*).
You passing the dynarr value (null, in this case) that will be overwrited by new int n BUT locally in the read function. The origina dynarr will not change.
You can solve in different ways, using a pointer to pointer or, better, a reference to pointer.
Declare the arr argument of read as int *&arr (means a reference to pointer to int) and all will works because in read function you will use a reference to original pointer and not more a copy.
+ 3
Passing the <arr> argument as reference seems to work ...
Line 4
void read( int n, int*& arr )
+ 2
KrOW Thank you for the explanation !
+ 1
ChillPill 🌶
Your help is needed here.
Could you help me out ?
+ 1
Ipang Indeed, it works, thank you !
+ 1
Maisu You are welcome
0
Quien me puede ayudar con este trabajo
0
Katie Andrade Esta pregunta ya ha sido respondida