The upper() function takes the str[] array and changes the values of the characters stored in it since str is just a pointer. So, is it correct to think of passing an array to a function as implicitly passing by-reference? But if one were to pass a string class variable to a function (e.g string myString), one would have to explicitly pass it by-reference (string&)?
Right both times. The only way to pass an array is by reference, it's because an array is a constant pointer so you are actually copying the memory address to the parameter, just like with pass-by-reference. Since a string is actually an object, it gets copied to the parameter like an int would (sort of). So, if you want to modify the argument, you have to pass a string variable by reference. 100% correct.
Sure. You should be able to. Just double check that the dynamically allocated array has enough room. I can't remember if strlen counts the null terminator or not, but I don't think it does. Also, if your parameter gives you problems, you could use const char* str instead.
if nobody said it today, thanks professor hank.
Thank you. I needed to hear that today.
thank you so much for these videos. with finals coming up these videos are so helpful to learn material I never understood in class!
Happy to hear that!
The upper() function takes the str[] array and changes the values of the characters stored in it since str is just a pointer. So, is it correct to think of passing an array to a function as implicitly passing by-reference?
But if one were to pass a string class variable to a function (e.g string myString), one would have to explicitly pass it by-reference (string&)?
Right both times.
The only way to pass an array is by reference, it's because an array is a constant pointer so you are actually copying the memory address to the parameter, just like with pass-by-reference.
Since a string is actually an object, it gets copied to the parameter like an int would (sort of). So, if you want to modify the argument, you have to pass a string variable by reference.
100% correct.
Hi professor,
I'm trying to make a function where I send a c-string as a literal and it returns the lowercase string.
something like this:
cout
Sure. You should be able to. Just double check that the dynamically allocated array has enough room. I can't remember if strlen counts the null terminator or not, but I don't think it does.
Also, if your parameter gives you problems, you could use const char* str instead.