zoukankan      html  css  js  c++  java
  • C语言中的数组的访问方式

      闲下来,写的代码,很是简单,不解释,代码如下:

      

      1 #include <stdio.h>
      2 
      3 int main(int argc, char **argv)
      4 {
      5         char cArray[] = "Hello,World!";
      6 
      7         for(int i = 0; i < 12; i++){
      8                 printf("the cArray[%d]: %c", i, cArray[i]);
      9         }
     10 
     11         for(int i = 0; i < 12; i++){
     12                 printf("%d[the cArray]: %c
    ", i, i[cArray]);
     13         }
     14 
     15         return 0;
     16 }

      执行结果为:

    the cArray[0]: H
    the cArray[1]: e
    the cArray[2]: l
    the cArray[3]: l
    the cArray[4]: o
    the cArray[5]: ,
    the cArray[6]: W
    the cArray[7]: o
    the cArray[8]: r
    the cArray[9]: l
    the cArray[10]: d
    the cArray[11]: !
    0[the cArray]: H
    1[the cArray]: e
    2[the cArray]: l
    3[the cArray]: l
    4[the cArray]: o
    5[the cArray]: ,
    6[the cArray]: W
    7[the cArray]: o
    8[the cArray]: r
    9[the cArray]: l
    10[the cArray]: d
    11[the cArray]: !

      将代码改为:

    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
            char cArray[] = "Hello,World!";
    
            for(int i = 0; i < 12; i++){
                    printf("%c", cArray[i]);
            }
            printf("
    ");
    
            for(int i = 0; i < 12; i++){
                    printf("%c",  i[cArray]);
            }
            printf("
    ");
            printf("%s
    ", cArray);
    
            return 0;
    }

      代码结果:

    Hello,World!
    Hello,World!
    Hello,World!
      关于数组的总结为:

    1、可以使用数据名加下标访问,也可以使用下标加数组名访问

    2、数组下标从0开始,不越界访问,需要程序员自己把握

    3、字符数组可以直接赋值成字符串内容

  • 相关阅读:
    阅读笔记09
    阅读笔记08
    阅读笔记07
    阅读笔记06
    阅读笔记05
    有关eclipse连接SQL Server 2008的问题
    每周进度条05
    软件需求模式阅读笔记04
    每周进度条04
    软件需求模式阅读笔记03
  • 原文地址:https://www.cnblogs.com/guochaoxxl/p/6925503.html
Copyright © 2011-2022 走看看