Passing string through a function (C programming)

47,086

Solution 1

That's because the type you need here simply is "pointer to char", which is denoted as char *, the asterisk (*) is part of the type specification of the parameter. It's not a "pointer to pointer to char", that would be written as char **

Some additional remarks:

  1. It seems you're confusing the dereference operator * (used to access the place where a pointer points to) with the asterisk as a pointer sign in type specifcations; you're not using a dereference operator anywhere in your code; you're only using the asterisk as part of the type specification! See these examples: to declare variable as a pointer to char, you'd write:

    char * a;
    

    To assign a value to the space where a is pointing to (by using the dereference operator), you'd write:

    *a = 'c';
    
  2. An array (of char) is not exactly equal to a pointer (to char) (see also the question here). However, in most cases, an array (of char) can be converted to a (char) pointer.

  3. Your function actually changes the outer char array (and passes back a pointer to it); not only will the uppercase of what was entered be printed by printf, but also the variable word of the main function will be modified so that it holds the uppercase of the entered word. Take good care the such a side-effect is actually what you want. If you don't want the function to be able to modify the outside variable, you could write char* upper(char const *word) - but then you'd have to change your function definition as well, so that it doesn't directly modify the word variable, otherwise the Compiler will complain.

Solution 2

char upper(char c) would be a function that takes a character and returns a character. If you want to work with strings the convention is that strings are a sequence of characters terminated by a null character. You cannot pass the complete string to a function so you pass the pointer to the first character, therefore char *upper(char *s). A pointer to a pointer would have two * like in char **pp:

char *str = "my string";
char **ptr_to_ptr = &str;
char c = **ptr_ptr_ptr; // same as *str, same as str[0], 'm'

upper could also be implemented as void upper(char *str), but it is more convenient to have upper return the passed string. You made use of that in your sample when you printf the string that is returned by upper.

Just as a comment, you can optimize your upper function. You are calling strlen for every i. C strings are always null terminated, so you can replace your i < strlen(word) with word[i] != '\0' (or word[i] != 0). Also the code is better to read if you do not compare against 96 and 123 and subtract 32 but if you check against and calculate with 'a', 'z', 'A', 'Z' or whatever character you have in mind.

Share:
47,086
Shashwat Black
Author by

Shashwat Black

“If you must choose between Two Evils, pick the one you’ve never tried before.”

Updated on March 29, 2020

Comments

  • Shashwat Black
    Shashwat Black about 4 years

    I have just started learning pointers, and after much adding and removing *s my code for converting an entered string to uppercase finally works..

    #include <stdio.h>
    char* upper(char *word);
    int main()
    {
        char word[100];
        printf("Enter a string: ");
        gets(word);
        printf("\nThe uppercase equivalent is: %s\n",upper(word));
        return 0;
    }
    
    char* upper(char *word)
    {
        int i;
        for (i=0;i<strlen(word);i++) word[i]=(word[i]>96&&word[i]<123)?word[i]-32:word[i];
        return word;
    }
    

    My question is, while calling the function I sent word which is a pointer itself, so in char* upper(char *word) why do I need to use *word?

    Is it a pointer to a pointer? Also, is there a char* there because it returns a pointer to a character/string right?
    Please clarify me regarding how this works.