总结一下C语言中的数组

1.关于数组的定义

 

类型  数组名[常量表达式]

 

定义一个长度可变的数组

 

Example1:   错误

 

<span style="color:#333333;">#include<stdio.h>
#include<stdlib.h>
int main(){

    
    int n = 10;
    int a[n] = {0};
        

    system("pause");
  return 0;
}
</span>


 

Example2: 

 

#include<stdio.h>
#include<stdlib.h>
int main(){

      
    const int n = 4; //此处n为符号常量
    int a[n] = {1,2,3,4};
    printf("%d",a[0]);
    printf("%d",a[1]);
    printf("%d",a[2]);
    printf("%d",a[3]);

    system("pause");
  return 0;
}

 

 

Example3:

 

#include<stdio.h>
#include<stdlib.h>
#define N 4
//此处为预定义 一个标识符 N
int main(){

    int a[N] = {1,2,3,4};
    printf("%d",a[0]);
    printf("%d",a[1]);
    printf("%d",a[2]);
    printf("%d",a[3]);



    system("pause");
  return 0;
}

 

2.数组的初始化

2.1   

int a[10] = { 1,   2 ,  3 , 4 , 5 , 6 , 7 , 8 ,  9 , 10};

#include<stdio.h>
#include<stdlib.h>
int main(){

    int a[4];

    printf("%d %d %d %d", a[0], a[1], a[2], a[3]);

    system("pause");
  return 0;
}


 

为了程序的安全性,在VC环境下,int a[4] 没有初始化,系统给该数组中每个元素默认一个数


2.2

以下正确

#include<stdio.h>
#include<stdlib.h>
int main(){

    int a[] = {1,2,3,4};

    printf("%d %d %d %d", a[0], a[1], a[2], a[3]);

    system("pause");
  return 0;
}

2.3
在很多编译器下
<img src="https://img-blog.csdn.net/20150204175144000?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG1hY2thbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
 

2.4

 


 

2.5

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值