Thread: Passing a string to a function - simple question

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    29

    Passing a string to a function - simple question

    A simple function that converts lowercase to uppercase.

    insert
    Code:
    This is what was already given:
    
    1	void Cap(char str[]);
    2	int main() {
    3		char string[31];
    4		printf("Enter a string of 30 characters or less: ");
    5		scanf("%30[^\n]", string);
    6		printf("Capitalized: %s\n", Cap(string));
    	}
    
    This is what I wrote:
    
    void Cap(char str[]){
    int i=0;
    while (str[i]){
    if (str[i]>=97 && str[i]<=122){
    str[i]-=32;
    }
    else i++;
    }}
    ======
    compiler keeps saying 'invalid use of void expression' in line 6.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You are trying to print the return value of a void function; what do you think should be printed?
    Code:
    printf("Capitalized: %s\n", Cap(string));
    Likely you mean this
    Code:
    printf("Capitalized: %s\n", string);
    Tim S.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    29
    So how am I supposed to call the function?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Cap changes the argument you give it. So, at whatever point in time you want string to become capitalized, you should call Cap. Since it doesn't return anything (merely changes the character array), you can't use it in a context of a thing. So: just as you use printf as an action (as opposed to a thing), you should use Cap as an action.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    29
    alright guys, thank you for your responses. still cant understand how can i make this piece of code work. if i include it inside main then it works just fine but i am supposed to use an external function for it. and thanks for that notion of thing and action, i find it interesting, have to think about, do more reading and internet research.

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    You could pass a string to Cap() like this: Cap(&string);. Or you could make string a global variable.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you have to think, you've already overthought the problem. There's nothing wrong with the way you call the function, you just have to actually call the function before you need the result. Take the function call out of the printf and make it a line of its own.

    EDIT: And if that's a problem (i.e., if you're not supposed to change those six numbered lines in your post), then the task is simply impossible.
    Last edited by tabstop; 07-25-2011 at 05:33 PM.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Babkockdood View Post
    You could pass a string to Cap() like this: Cap(&string);. Or you could make string a global variable.
    Neither... he's got the call more or less correct... it's calling it inside the printf() that's his problem.

    To call it inside the printf() he could do this...
    Code:
    char *Cap(char *str)
    {
      int i=0;
      while (str[i])
        if (str[i]>=97 && str[i]<=122)
          str[i]-=32;
        else 
         i++;
    return str; }

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    29
    Thank you tabstop, that solved the problem. I just inserted Cap(string) before printf. And thank you CommonTater but I am not allowed to change the function prototype. I think the teacher wont mind if there's one extra line of code. And thank you all guys.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by vegan View Post
    Thank you tabstop, that solved the problem. I just inserted Cap(string) before printf. And thank you CommonTater but I am not allowed to change the function prototype. I think the teacher wont mind if there's one extra line of code. And thank you all guys.
    Actually what I wrote is the same as the prototype... I just used a notation I'm more accustomed to. Either str[] or *str should work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple question on passing parameters
    By mickey0 in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2007, 09:28 AM
  2. Passing by Reference. Simple question.
    By d3m105 in forum C Programming
    Replies: 6
    Last Post: 10-31-2007, 12:47 PM
  3. ! C question: simple integer function passing
    By aaronc in forum C Programming
    Replies: 10
    Last Post: 04-29-2004, 01:18 AM
  4. Passing string through a function...
    By MisterWonderful in forum C++ Programming
    Replies: 5
    Last Post: 04-15-2003, 01:33 PM
  5. Passing string to function
    By PotitKing in forum C Programming
    Replies: 8
    Last Post: 05-24-2002, 05:17 PM