A question related to strcmp

This is a discussion on A question related to strcmp within the C++ Programming forums, part of the General Programming Boards category; Here is a program I wrote to construct a suffix array. Code: int pstrcmp(const void* str1, const void* str2){ int ...

  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
    21,313
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 05: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, 10: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

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