Thread: String Compare manual built

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    String Compare manual built

    Hi, I just tried to make a manual Comparing strings by using this code:
    Code:
    int compare(const void* i, const void* j);
    int main()
    {
    	int i = compare("hi", "hi");
    	printf("%d", compare);
    }
    int compare(const void* i, const void* j)
    {
    	return (strcmp(*(char**)i, *(char**)j));
    }
    it doesn't work!, any help? another thing I'm still confusing on type casting a variable (char**) i , can someone explain me it ?! thanks

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Have a closer look at line 5.
    Do you want to print the address of the compare function?

    There is no need for casting:
    Code:
    int compare(const void* i, const void* j)
    {
        return strcmp(i, k);
    }
    Of course you could write it like this:
    Code:
    int compare(const void* i, const void* j)
    {
        const char *cp1 = i;
        const char *cp2 = j;
        
        return strcmp(cp1, cp2);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 05-05-2014, 07:39 AM
  2. Built in Conversion from Integer to Binary String?
    By dleach in forum C Programming
    Replies: 20
    Last Post: 08-03-2013, 10:47 AM
  3. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  4. manual string reverse
    By phoneix_hallows in forum C Programming
    Replies: 10
    Last Post: 08-27-2009, 07:40 PM
  5. Replies: 8
    Last Post: 10-21-2007, 01:38 PM

Tags for this Thread