[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 @   ABeen  阅读(1241)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示