passing character arrays into function

This is a discussion on passing character arrays into function within the C Programming forums, part of the General Programming Boards category; Hello. For this program I'm doing I'm trying to get a user to enter two words, have those words passed ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    31

    passing character arrays into function

    Hello. For this program I'm doing I'm trying to get a user to enter two words, have those words passed into a function which compares the two words and sets a char pointer to a certain sentence based on the comparison.

    The error I'm getting is "too few arguments to function compare". Any idea what I'm doing wrong?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    void compare(char string1[30], char string2[30]);
    void main()
    {
            char string1[30];
            char string2[30];
    
    
            printf("Please enter a word.\n");
            scanf("%s", string1);
            printf("Please enter a second word.\n");
            scanf("%s", string2);
    
    
            compare();
    
    
    }
    
    
    void compare(char string1[30], char string2[30])
    {
    char *msg = NULL;
    
    
            if(strcmp(string1, string2)>0)
            { msg = "First word is greater than the second."; }
            else if(strcmp(string1, string2)<0)
            { msg = "Second word is greater than the first."; }
            else
            { msg = "First word is equal to the second word."; }
    
    
    
    
    printf("%s\n", msg);
    }
    Thanks!

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Your function prototype says that "compare" takes two character arrays as its arguments. But then when you call it, you don't give it any arguments at all.

    I think you want to do
    Code:
    compare( string1, string2 );
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The call to compare requires (string1, string2) (the names of the arrays).

    You're too quick, BigH!

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    Yep that fixed it! Thanks you two!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing character array into function
    By cactuar in forum C++ Programming
    Replies: 9
    Last Post: 10-23-2009, 02:10 PM
  2. passing character to a function
    By RedWind in forum C Programming
    Replies: 11
    Last Post: 12-02-2008, 12:25 PM
  3. Passing pointer to character to function
    By alexpos in forum C Programming
    Replies: 1
    Last Post: 11-19-2005, 10:18 PM
  4. scanf(), Parameter Passing, and Character Arrays
    By linguae in forum C Programming
    Replies: 2
    Last Post: 05-02-2005, 04:19 AM
  5. Passing a character array to a function
    By Qchem in forum C Programming
    Replies: 3
    Last Post: 03-07-2005, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21