Thread: A question related to strcmp

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    A question related to strcmp

    Here is a program I wrote to construct a suffix array.

    Code:
    int pstrcmp(const void* str1, const void* str2){
    	int tmp = strcmp((char*)str1, (char*)str2);
    	return tmp;
    }
    
    int main(){
    
    	char str[]="to be or not to be this is a problem.";
    	char **a = (char**) malloc(sizeof(char*)*strlen(str));
    
    	for(int i=0;i<strlen(str);i++){
    		a[i] = &str[i];
    	}
    
    	qsort(a,strlen(str),sizeof(char*),pstrcmp);
    	for(int i=0;i<strlen(str);i++) cout<<a[i]<<endl;
    
            return 0;
    }
    It seems pstrcmp doesn't work.... When I debug the pstrcmp, the tmp value is always -1! Anybody knows what's wrong?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Hold on, did you intend to post this in the C programming forum?

    EDIT:
    oops, sorry. I didnt look carefully. It does look like it was written to be a C program at a glance
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    FYI: Array A contains address of the following strings.

    to be or not to be this is a problem.
    o be or not to be this is a problem.
    be or not to be this is a problem.
    be or not to be this is a problem.
    e or not to be this is a problem.
    or not to be this is a problem.
    or not to be this is a problem.
    r not to be this is a problem.
    not to be this is a problem.
    not to be this is a problem.
    ot to be this is a problem.
    t to be this is a problem.
    to be this is a problem.
    to be this is a problem.
    o be this is a problem.
    be this is a problem.
    be this is a problem.
    e this is a problem.
    this is a problem.
    this is a problem.
    his is a problem.
    is is a problem.
    s is a problem.
    is a problem.
    is a problem.
    s a problem.
    a problem.
    a problem.
    problem.
    problem.
    roblem.
    oblem.
    blem.
    lem.
    em.
    m.
    .
    Last edited by meili100; 07-07-2007 at 12:44 PM.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Consider the string "abracadabra", of length 11. It has eleven suffixes: "abracadabra", "bracadabra", "racadabra", and so on down to "a". Sorted into lexicographical order, these suffixes are

    a
    abra
    abracadabra
    acadabra
    adabra
    bra
    bracadabra
    cadabra
    dabra
    ra
    racadabra

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Your cast inside the compare function is wrong.

    *(char**)str1
    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.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thank you! It works. But I still don't understand why is this. Each element in array A is char*, isn't it?

    Quote Originally Posted by Salem View Post
    Your cast inside the compare function is wrong.

    *(char**)str1

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    If each element of your array is a 'T', then the qsort compare function receives two 'T*' pointers.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 06:30 PM
  2. A question about strcmp...
    By krsauls in forum C Programming
    Replies: 6
    Last Post: 05-02-2007, 04:39 AM
  3. Question related to getpid and getppid
    By g_p in forum C Programming
    Replies: 4
    Last Post: 12-18-2006, 11:35 AM
  4. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  5. Question about strcmp
    By readerwhiz in forum C Programming
    Replies: 1
    Last Post: 09-23-2001, 05:18 PM