C语言基础5

时间:2014-11-03 19:19:02   收藏:0   阅读:183

二维数组的定义:

数据类型 数组名称 [常量表达式1] [常量表达式2] = {.....}

int a[2][3] ={

  {4,5,6},

  {7,8,0}                 //或者{7} 后面不写8和9 ,系统会默认的帮你添加0{7,0,0}

};

OR:

int b[3][2] ={3,87,43,66,82,11,34 };

OR:

int b[][2] ={3,87,43,66,82,11,34 };    //横可以不写

 字符串数组

char 数组名称 [字符串个数][每个字符串允许存放的最大值] ={....}

char  str[3][15] ={"ipad","ipod","iphone"};

字符串数组本质上是一个二维数组,访问某个字符串使用数组名称[第一维下标].

每个字符串的长度不能超过第二维度的长度-1。 

 比如说:

char str2[3][20] ={"luoshuailuotingluomama","ipod","ipad"};

第二维度为20,20-1 =19,但是第一个字符串就超过了19.

    char str2[3][20] ={"luoshuailuotingluomamadddddddssssssss","ipod","ipad"};     //不会报错,但是会警告.

     for (int i =0 ; i < 3; i++ ) {

        printf("The end of Result :%s\n",str2[i]);

    }

思考一个问题 :  

创建一个字符串数组,对字符串 (英?)从小到大排序 

  char  str[5][15] ={"ipad","ipod","iphone","main","luoshuai"};

  char temp[] ={0};

  for(int i =0 ;i < strlen(str); i++ ){  

    for(int j =0 ;j<strlen(str)-i-1;j++){

      if(  strcmp( str[j] > str[j+1] )  > 0 ){  

         strcopy(temp,str[j]);

         strcopy(str[j],str[j+1]);

         strcopy(str[j+1],temp);

      }

}

}

for(int j =0 ;j<strlen(str);j++){  

  printf("The end of Result : %s\n",str[j]);

}

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!