string pointer passed to a function help

This is a discussion on string pointer passed to a function help within the C Programming forums, part of the General Programming Boards category; I'm not so familiar with functions and function prototypes. I get compiler errors which is the line where I attempt ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    38

    string pointer passed to a function help

    I'm not so familiar with functions and function prototypes. I get compiler errors which is the line where I attempt to pass the strings to a function

    Type error in argument 1 to 'sort'; expected 'char *' but found 'char'
    Type error in argument 2 to 'sort'; expected 'char *' but found 'char'
    Type error in argument 3 to 'sort'; expected 'char *' but found 'char'
    Type error in argument 4 to 'sort'; expected 'char *' but found 'char'

    Code:
     #include <stdio.h>
    #include <string.h>
    
    void sort (char *,char *,char *,char *);
    
    int main()
    {
    
    	char *str1 = {"Florida"};
    	char *str2 = {"Oregon"};
    	char *str3 = {"California"};
    	char *str4 = {"Georgia"};
    	
    	printf("Four states in alphabetical order");
    	sort(*str1, *str2, *str3, *str4);
    }
    
    void sort (char *str1,char *str2, char *str3, char *str4)
    {
    	if (strcmp(str3, str1) >0) 
    		printf("\n%s", str3);
    	if (strcmp(str1, str4) >0)
    		printf("\n%s", str1);
    	if (strcmp(str4, str2) >0)
    		printf("\n%s", str1);
    	if (strcmp(str2, str4) <0)
    		printf("\n%s\n", str1);
    
    }
    /*
    Build a program that uses an array of strings to store the following
    names:
    • “Florida”
    • “Oregon”
    • “California”
    • “Georgia”
    Using the preceding array of strings, write your own sort()
    function to display each state’s name in alphabetical order using
    the strcmp() function. */

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    str1 is the address of the first character in the str1[] array. If you pass *str1 to the function, you are passing only that initial character rather than the whole string. Remove the * or, if it makes it easier to understand, pass it &str1[0].

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    38
    I have tried removing the * already and the sort function doesn't print anything if I do. I tried again and the same result. Not sure how to write out the other part you said in the compiler.

  4. #4
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Quote Originally Posted by BrandNew View Post
    I have tried removing the * already and the sort function doesn't print anything if I do. I tried again and the same result. Not sure how to write out the other part you said in the compiler.
    All of your if statements in your sort function evaluate to false, thus nothing prints..
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    38
    Thanks

    I was basing my if conditions from the example in the book but that example has a different value of course

    Code:
     #include <stdio.h>
    #include <string.h>
    
    void sort (char *,char *,char *,char *);
    
    int main()
    {
    
    	char *str1 = {"Florida"};
    	char *str2 = {"Oregon"};
    	char *str3 = {"California"};
    	char *str4 = {"Georgia"};
    	
    	printf("Four states in alphabetical order");
    	sort(str1, str2, str3, str4);
    }
    
    void sort (char *str1,char *str2, char *str3, char *str4)
    {
    	if (strcmp(str3, str1) <0) 
    		printf("\n%s", str3);
    	if (strcmp(str1, str4) <0)
    		printf("\n%s", str1);
    	if (strcmp(str4, str2) <0)
    		printf("\n%s", str4);
    	if (strcmp(str2, str4) >0)
    		printf("\n%s\n", str2);
    
    }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    The problem is, you need to do more comparisons.
    In just 4 comparisons, the most you can work out is which one is first. It won't be enough to tell you about the other three.

    Also, sorting gets more efficient if you're allowed to re-order the elements, and to do this, an array is most useful.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Yep, you may want to use an array and make your sort a simple sorting algorithm like bubble sort.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,501
    Code:
    	if (strcmp(str4, str2) <0)
    		printf("\n%s", str4);
    	if (strcmp(str2, str4) >0)
    		printf("\n%s\n", str2);
    Also note that those two if statements are equivalent. The parameter order matters with strcmp:
    Quote Originally Posted by man page
    SYNOPSIS
    int strcmp(const char *s1, const char *s2);

    DESCRIPTION
    The strcmp() function compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than,
    to match, or be greater than s2.
    Switching both the parameter order and comparison cancels out the changes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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