[ASM C/C++] C语言数组

固定长度的数组:

  可以具有任何的存储类别。

长度可变的数组:

  只能具有自动的生存周期(定义于语句块内,眀没有static修饰符)。

  名字必须为一般的标识符,因此结构或联合的成员不能是数组的标识符。

读写数组数据可按索引和指针两种方法。

 

#include <stdio.h>


int a[10];        //具有外部链接
static int b[10]; //具有静态的生存周期和作用域


void parray(int count, int arr[])
{
    //索引访问
    for(int i=0; i<count; i++)
    {
        printf("the %d is: %d \n", i, arr[i]);
    }

    //指针访问
    for(int *p = arr; p < arr + count; ++p)
    {
        printf("p the value is: %d \n", *p);
    }
}


int  main(int argc, char *argv[])
{
    static int c[10]; //静态生存周期和语句块作用域
    //int d[10];      //具有自的生存周期和语句块作用域

    int vla[2*argc];  //自动生存周期,长度可变的数组
    //error: variable length array declaration can not have 'static' storage duration
    //static int e[n];  //精态生存周期,不能为长度可变数组
    //error: fields must have a constant size: 'variable length array in structure' extension will never be supported
    //struct S { int f[n] }; //小标签不能为长变可变数组名
    struct S { int f[10]; }; //小标签不能为长变可变数组名
    //多维数组
    extern int a2d[][5];
    //int a3d[2][2][3] = {{{110,111,112},{120,121,122}},{{210,211,212},{220,221,222}}};
    //int a3d[][2][3]  = {{{1},{4}},{{7,8}}};
    //int a3d[2][2][3] = {{1,0,0,4}, {7,8}};
    //int a3d[2][2][3] = {1, [0][1][0]=4, [1][0][0]=7,8};
    int a3d[2][2][3] = {{1}, [0][1]=4, [1][0]={7,8}};


    //初始化
    //int d[] = {1,2,3};
    //d[0] = 1, d[1]=2, d[2]=3;
    int d[10] = {1,2,3,[6]=6}; //初始化特定元素
    parray(10, d);
    
    return 1;
}                                                                                                                                                                        

 

posted @ 2015-01-28 22:45  ABeen  阅读(1199)  评论(0编辑  收藏  举报