C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-07-2007, 12:16 PM   #1
Registered User
 
Join Date: Apr 2007
Posts: 282
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?
meili100 is offline   Reply With Quote
Old 07-07-2007, 12:27 PM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 07-07-2007, 12:38 PM   #3
Registered User
 
Join Date: Apr 2007
Posts: 282
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.
meili100 is offline   Reply With Quote
Old 07-07-2007, 01:12 PM   #4
Registered User
 
Join Date: Apr 2007
Posts: 282
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
meili100 is offline   Reply With Quote
Old 07-07-2007, 01:14 PM   #5
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 07-07-2007, 01:39 PM   #6
Registered User
 
Join Date: Apr 2007
Posts: 282
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
meili100 is offline   Reply With Quote
Old 07-07-2007, 02:51 PM   #7
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:16 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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