Opening the book…
In this study case, we demonstrate another application of pointer in C++ programming.
Example
Write a function that receives an character array (a C-string) through its parameter. The function makes a copy of the array it receives and then changes all the characters in the copy to their upper case counterparts. The function returns the copy of the array without altering the data in the original array it receives.
Here is the code of the function along with main(), which is for testing purposes:

Line 4: This is the header of the function. We specify the return of the function is a character pointer by placing '*' in front of the name of the function. The parameter of the function is char a[], which indicates the input of the function is a character array (a C-string).
Line 6 - Line 11: We use a while loop to count the number of the characters in the array the function receives which is array a[].
Line 12: Here we declare a character pointer copy and use a new-statement to dynamically allocate another array with length being the same as a[].
Line 13 - Line 16: We use a for loop statement to copy each character in a[] into the new array copy[] (Line 14) and at the same time, change each character in the array copy[] to their upper case counterparts (Line 15).
Line 17: The function returns the array copy.
Line 20 - Line 30 is main() where we test the function capitalize().
Line 22: We create a sample string in main()
Line 23: We declare a character pointer cap
Line 24: The function is called and the function receives array a as its input. The function makes a copy of the array a and changes all the characters in array copy to their upper case counterparts. The function return the array copy (Actually, the function returns the address of the first element of the array copy, and we put the return address into the pointer cap, thus cap becomes the name of the array the function returns.
Following the screenshot of a test run of this program:

This page titled 9.3: Case Study- Array Copy was last modified on Wed, 24 Dec 2025 20:37:20 GMT and is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Jin He.
The book could not be loaded just now.