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\n", 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("\n");

        for(int i = 0; i < 12; i++){
                printf("%c",  i[cArray]);
        }
        printf("\n");
        printf("%s\n", cArray);

        return 0;
}

  代码结果:

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

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

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

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

转载于:https://www.cnblogs.com/guochaoxxl/p/6925503.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值