I'm currently revising on C pointers for an upcoming test and I came across a rather interesting program as I was running through the lecture slides.
#include <stdio.h>
#include <ctype.h>
void convertToUppercase(char *sPtr);//function prototype, input is a dereferenced pointer
int main(void)
{
char string[] = "cHaRaCters and $32.98";
printf("The string before conversion is %s\n", string);
convertToUppercase(string); //Why can I just input the string array into the function? I don't need a pointer?
printf("The string after conversion is %s", string);
}
void convertToUppercase(char *sPtr)
{
while (*sPtr != '\0')//Clearly see the NULL terminating character, means the entire array is passed to the function.
{
*sPtr = toupper(*sPtr);
sPtr++;
}
}
What I don't understand is this particular line: convertToUppercase(string);
The function prototype requires a (char *sPtr) as an input, but for this case, the string array is just passed into the function call. How?
I don't really understand how this works. Can anyone help me to understand this code better?
My guess is that the array name itself 'string' already contain the address of the entire array itself (That's why when we assign pointers to array we don't put the '&')
Thank you in advance, take care!