使用二级指针对字符串数组进行排序(其中冒泡法排序部分是摘抄)有2个问题
程序代码:
#include <stdio.h> #include <string.h> #define num 10 int main(void) { char* test[]={"All entries of the table must "," values of x and y(x) printed as ", "when mainframes were ","render any typical double ", "the summation process note ","repeated to produce sequences ", "summing the series above","This process of finding ", "convergence of the summation ","Numerical Summation of a Series"}; char** p=test; char* tmp=NULL; int i,j,k; //以下用冒泡法对字符串数组进行排序 for(i=0;i<num-1;i++) { k=i; for(j=i+1;j<num;j++) { if(strcmp(*(p+k),*(p+j))>0) k=j; if(k!=i) { tmp=*(p+k); *(p+k)=*(p+i); *(p+i)=tmp; } } } /////////////////////////////////////////////////////////////////////////////////// //输出排序后的字符串数组 for(i=0;i<num;i++) { printf("源串=%-40s 长度=%d\n",*p,(int)strlen(*p)); p++; } /////////////////////////////////////////////////////////////////////////////////// return 0;F:\c_source\t2\Debug>fc
源串= values of x and y(x) printed as 长度=33
源串=All entries of the table must 长度=30
源串=Numerical Summation of a Series 长度=31
源串=convergence of the summation 长度=29
源串=This process of finding 长度=24
源串=render any typical double 长度=26
源串=repeated to produce sequences 长度=30
源串=summing the series above 长度=24
源串=the summation process note 长度=27
源串=when mainframes were 长度=21
问题1、样例输出的粉色部分 明显不对,不知道代码差哪了(我对排序不熟悉)
问题2、
看这几句交换代码
tmp=*(p+k);
*(p+k)=*(p+i);
*(p+i)=tmp;
我认为,这样的代码交换类似int一类的还可以,交换字符串指针 怎么还可以呢
数组中字符串位置的变动 应该使用类似字符串拷贝一类的东东才能完成啊