指针数组应用实例
用指针数组实现下列功能:
1、在main函数中输入5个字符串
2、在main函数中将字符串按照从小到大的顺序排列输出。
源码:
#define N 5
#include "stdio.h"
#include "malloc.h"
#include "string.h"
int main()
{
?int i,j;
?char * p[N];
?char *temp;
?for(i=0;i<N;i++)
?{
??printf ("输入的第%d个字符串是\n",i+1);
??p[i]=(char *)malloc(sizeof(char)*20);//定义了指针后要使其指向一个有效的空间!!
??scanf("%s",p[i]);
?}
?? for (i=0;i<N;i++)
?? {
??? for (j=i+1;j<N;j++)
??? {
???? if ((strcmp(p[i],p[j]))>0)
???? {
????? temp=p[j];
????? p[j]=p[i];
????? p[i]=temp;
?????
???? }
??? }
?? }
??
?? for (i=0;i<N;i++)
?? {
????? printf ("输入的第%d个字符串是%s\n",i+1,p[i]);
?? }
?return 0;
}