Arrays are passed by reference since they are pointers that point to index 0. If that is the case, why does this programmer define a pointer char *sPtr as a parameter of function convertToUppercase() if he could just use char string [] as a parameter and have the values in the function main changed. Do both ways perform the same task or are they substantially different? Thanks!
#include <stdio.h>
#include <ctype.h>
void convertToUppercase(char *sPtr );
int main( void )
{
char string[] = "characters and $32.98"; /* initialize char array */
printf( "The string before conversion is: %s", string );
convertToUppercase( string );
printf( "\nThe string after conversion is: %s\n", string );
return 0;
}
/* convert string to uppercase letters */
void convertToUppercase(char *sPtr)
{
while (*sPtr != '\0' ) { /* current character is not '\0' */
if ( islower( *sPtr))
{
*sPtr = toupper( *sPtr );/* convert to uppercase */
}
++sPtr;
} /* end while */
} /* end function convertToUppercase */