![]() |
| | #1 |
| Registered User Join Date: Apr 2007
Posts: 282
| A question related to strcmp 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;
}
|
| meili100 is offline | |
| | #2 |
| C++ Witch 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 | |
| | #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 | |
| | #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 | |
| | #5 |
| and the hat of vanishing 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 | |
| | #6 |
| Registered User Join Date: Apr 2007
Posts: 282
| |
| meili100 is offline | |
| | #7 |
| and the hat of vanishing 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |