0

Can someone help me understand pointers

I am a little bit confused with pointers. I made a code where i made a function that makes a string array. The code gives an error if i dont use and * in the parameters of the functions. I know that a function cannot create an array but if you do it like that does the function create an array and then points to the address of the variable and changes value that is currently in that address Here is my code https://code.sololearn.com/cRIqt0Atiihn/?ref=app

13th Oct 2022, 1:56 PM
Aidan H
Aidan H - avatar
5 Answers
+ 2
Hi Aidan, What sort of output did you expect from convert() and makeArray()? can you illustrate briefly? As it is now, convert() seems to be where the problem is. But I'm not too sure what to suggest since it isn't clear to me, what you intend to do by the code.
13th Oct 2022, 2:23 PM
Ipang
+ 2
Okay Aidan Good to know you've got it
14th Oct 2022, 12:08 AM
Ipang
+ 1
Ipang Hey, its ok i have figured why it works So an array gives an address if you print it without an index e.g: int array[5]; cout << array; and a pointer also prints and address e.g: int * ptr; cout << ptr; so if you want to make an array function where you want to make an array of numbers or strings you can use a pointer in the function parameter and the array without the index when you call the function. void makeArray(int* i){ for(int x=0;x<5;x++){ i[x] = x; } } int main(){ int arr[5]; makeArray(arr); for(int x=0;x<5;x++){ cout << arr[x] << " "; } return 0; }
13th Oct 2022, 8:38 PM
Aidan H
Aidan H - avatar
0
Thank you for responding. Got abit lost in my explanation The code takes a string input. The input is seperated by a comma for length and height. Basically you have two rooms and you measure the rooms to see which room is bigger (room1) a = 2,5 or 21,5 (room2) b = 5,3 or 6,10 MakeArray() makes a string e.g "2,5" into a string array arr[2] = {"2","5"} and removes the "," And the convert() converts the string into an integer so that you will have an int array The code works fine, my question is just, why do i have to declare the parameters of the functions as pointers ? My understanding of pointers is not that great Ipang
13th Oct 2022, 2:48 PM
Aidan H
Aidan H - avatar
0
Ipang Why use a pointer instead of the value directly ? If that makes sense
13th Oct 2022, 2:55 PM
Aidan H
Aidan H - avatar