zoukankan      html  css  js  c++  java
  • 一级指针,二级指针,指向数组的指针

    对"black,green,yellow,pin,red"几个单词排序

    01

    #include <iostream.h>
    #include <string.h>
    void sort(char (*p)[20],int n)
    {
    	char *q;
    	char b[20];
    	q=b;
    	int i,j;
    	for(i=0;i<n;i++)
    	{
    		for(j=i+1;j<n;j++)
    		{
    			if(strcmp(p[i],p[j])>0)
    			{
    				strcpy(q,*(p+i));
    				strcpy(*(p+i),*(p+j));
    				strcpy(*(p+j),q);
    			}
    		}
    	}
    }
    void main()
    {
    	char a[5][20]={"black","green","yellow","pin","red"};
    	sort(a,5);
    	for(int i=0;i<5;i++)
    	{
    		cout<<a[i]<<endl;
    	}
    }
    

      02

    #include <iostream.h>
    #include <string.h>
    void sort(char *p[],int n)
    {
    	char *q;
    	int i,j;
    	for(i=0;i<n;i++)
    	{
    		for(j=i+1;j<n;j++)
    		{
    			if(strcmp(*(p+i),*(p+j))>0)
    			{
    				q=*(p+i);
    				*(p+i)=*(p+j);
    				*(p+j)=q;
    			}
    		}
    	}
    }
    void main()
    {
    	char a[5][20]={"black","green","yellow","pin","red"};
    	char *q[5];
    	for(int i=0;i<5;i++)
    	{
    		q[i]=a[i];
    	}
    	sort(q,5);
    	for(i=0;i<5;i++)
    	{
    		cout<<q[i]<<endl;
    	}
    }
    

      03

    #include <iostream.h>
    #include <string.h>
    void sort(char *p,int n)
    {
    	char *q;
    	char b[20];
    	q=b;
    	int i,j;
    	for(i=0;i<n;i++)
    	{
    		for(j=i+1;j<n;j++)
    		{
    			if(strcmp(p+i*20,p+j*20)>0)
    			{
    				strcpy(q,p+i*20);
    				strcpy(p+i*20,p+j*20);
    				strcpy(p+j*20,q);
    			}
    		}
    	}
    }
    void main()
    {
    	char a[5][20]={"black","green","yellow","pin","red"};
    	char *q;
    	q=&a[0][0];
    	sort(q,5);
    	for(int i=0;i<5;i++)
    	{
    		cout<<a[i]<<endl;
    	}
    }
    

      04

    #include <iostream.h>
    #include <string.h>
    void sort(char **p,int n)
    {
    	char *q;
    	int i,j;
    	for(i=0;i<n;i++)
    	{
    		for(j=i+1;j<n;j++)
    		{
    			if(strcmp(*(p+i),*(p+j))>0)
    			{
    				q=*(p+i);
    				*(p+i)=*(p+j);
    				*(p+j)=q;
    			}
    		}
    	}
    }
    void main()
    {
    	char a[5][20]={"black","green","yellow","pin","red"};
    	char *q[5];
    	for(int i=0;i<5;i++)
    	{
    		q[i]=a[i];
    	}
    	sort(q,5);
    	for(i=0;i<5;i++)
    	{
    		cout<<a[i]<<endl;
    	}
    }
    

      

  • 相关阅读:
    Linq系列:基础与本质(Part III)
    CLR系列:浅析委托
    8万亿 全球第二还是最二?
    CLR系列:窥视HashTable
    c#3.0系列:Anonymous Type In CLR(3.5)
    VC6 编译选项问题
    C++资源库不完全版本
    如何用C实现C++的特性
    几种用于WSN的仿真工具
    嵌入式编程的好资源
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4064821.html
Copyright © 2011-2022 走看看