Hey guys I always having confusion when to pass refrence as a parameter

Hey guys please help me to understand when we will pass address as refrence in function parameter and when will pass pointer as a parameter.

for ex: consider this https://www.geeksforgeeks.org/repeated-character-whose-first-appearance-is-leftmost/
In the Method 2 they pass string as a refrence . Please help me understood reference pass concept.

hi @rssrivastavarohan passing by reference is basically passing a pointer only. In C language the syntax is actually like

func(int *val) {
//do stuff
}

main(){
int val;
func(&val);
}

but it can be very complicated to use the values passed like these in the functions because you will have to use dereference pointer all the time, whenever you try to use that value (because “val” is a pointer in the function, not an int). So in c++ the syntax is made a little better by allowing us to write it like this
func(int &val) {
//do stuff
}

main(){
int val;
func(val);
}

now we can use “val” directly.

Coming to your question, arrays are basically pointers to the first element of the array. So you dont need to send them by reference, they are sent by reference by default. This is why you can also use a pointer while passing an array. But all the other datatypes need to be sent the conventional way because they are not pointers to anything.
Also, remember that “string” datatype and a character array may share some functionalities but they are not the same and hence some syntax differs.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.