C in ObjC系列(3) 阵列与指标

C in ObjC系列(3) 阵列与指标

C in ObjC系列为Objective-C中保留C语言语法的系列介绍。

阵列

阵列有个非常容易使用的索引值方式取得内容,利用此特性来完成连续性的动作时就能用回圈与索引值来完成,一般程式语言都能接受1维、2维这两种维度的阵列,3维就必需注意看看程式语言的编译器是否能支援了。

1维阵列

//-----------start-----------
    NSInteger integer_array[4];
    integer_array [0] = 0;
    integer_array [1] = 1;
    integer_array [2] = 2;
    integer_array [3] = 3;
    NSLog(@"integer_array");
    for (NSInteger m = 3; m >= 0; m --) {
        NSLog(@"%ld",integer_array[m]);
    }
//------------end------------

输出结果:

2014-08-17 20:19:38.114 LoopDemo[68436:303] integer_array
2014-08-17 20:19:38.115 LoopDemo[68436:303] 3
2014-08-17 20:19:38.115 LoopDemo[68436:303] 2
2014-08-17 20:19:38.115 LoopDemo[68436:303] 1
2014-08-17 20:19:38.115 LoopDemo[68436:303] 0

//-----------start-----------
    NSInteger integer_array1[] = {0,1,2,3};

    NSLog(@"integer_array1");
    for (NSInteger m = 3; m >= 0; m --) {
        NSLog(@"%ld",integer_array1[m]);
    }
//------------end------------

输出结果:

2014-08-17 20:19:38.116 LoopDemo[68436:303] integer_array1
2014-08-17 20:19:38.116 LoopDemo[68436:303] 3
2014-08-17 20:19:38.117 LoopDemo[68436:303] 2
2014-08-17 20:19:38.117 LoopDemo[68436:303] 1
2014-08-17 20:19:38.117 LoopDemo[68436:303] 0

2维阵列

2维阵列最常被使用在棋盘式的查表座标,例如:(3,2),阵列中就会以变数名[3][2](假定座标从0开始,索引值从0开始)。

//-----------start-----------
    NSInteger integer_2_dim_array[4][4] = {
        {0,0,0,0},
        {1,1,1,1},
        {2,2,2,2},
        {3,3,3,3}
    };

    for (NSInteger m = 3; m >= 0; m --) {
        for (NSInteger n = 3; n >= 0; n --) {
            NSLog(@"row:%ld column:%ld value %ld",m,n,integer_2_dim_array[m][n]);
        }
    }
//------------end------------

输出结果:

2014-08-17 22:58:49.008 LoopDemo[70342:303] row:3 column:3 value 3
2014-08-17 22:58:49.009 LoopDemo[70342:303] row:3 column:2 value 3
2014-08-17 22:58:49.010 LoopDemo[70342:303] row:3 column:1 value 3
2014-08-17 22:58:49.010 LoopDemo[70342:303] row:3 column:0 value 3
2014-08-17 22:58:49.010 LoopDemo[70342:303] row:2 column:3 value 2
2014-08-17 22:58:49.011 LoopDemo[70342:303] row:2 column:2 value 2
2014-08-17 22:58:49.011 LoopDemo[70342:303] row:2 column:1 value 2
2014-08-17 22:58:49.011 LoopDemo[70342:303] row:2 column:0 value 2
2014-08-17 22:58:49.011 LoopDemo[70342:303] row:1 column:3 value 1
2014-08-17 22:58:49.012 LoopDemo[70342:303] row:1 column:2 value 1
2014-08-17 22:58:49.012 LoopDemo[70342:303] row:1 column:1 value 1
2014-08-17 22:58:49.013 LoopDemo[70342:303] row:1 column:0 value 1
2014-08-17 22:58:49.013 LoopDemo[70342:303] row:0 column:3 value 0
2014-08-17 22:58:49.013 LoopDemo[70342:303] row:0 column:2 value 0
2014-08-17 22:58:49.013 LoopDemo[70342:303] row:0 column:1 value 0
2014-08-17 22:58:49.014 LoopDemo[70342:303] row:0 column:0 value 0

指标

指标的定义是在于宣告的变数名称前有*号为指标变数,例如:

//-----------start-----------
//资料型态 *指标名称
    int *ptrInt;
//------------end------------

指标变数名称宣告后它存放的只是记忆体的参考位址,所这样的宣告下,必需要配置占用的记忆体与指标变数做连结才能将它当做一般变数使用:

//-----------start-----------
//资料型态 *指标名称
    int *ptrInt = malloc(sizeof(int));
    *ptrInt = 3;

    NSLog(@"%x",*ptrInt);
//------------end------------

输出结果:

2014-08-17 23:22:55.414 LoopDemo[70825:303] 3

如果宣告指标变数后,未配置占用的记忆体,那你宣告的指标变数只能利用现有的变数将该变数的记忆体位址指定至宣告的指标变数,要取得该变数的存放位址时需要在变数前加上&

//-----------start-----------
    int *ptrInt;
    int val;
    val = 4;
    ptrInt = &val;//取得val记忆体位址并传至ptrInt
    *ptrInt = 5;//设定新的内容为5

    NSLog(@"*ptrInt %x val:%x",*ptrInt,val);
//------------end------------

val与ptrInt所指定相官的记忆体位址,所以我们变更ptrInt内容后,印出val值的结果会是相同。

输出结果:

2014-08-18 10:13:21.706 LoopDemo[73047:303] *ptrInt 5 val:5

当你宣告指标变数后,想要知道该指标变数的记忆体位址时将*拿掉:

//-----------start-----------
    int *ptrInt = malloc(sizeof(int));
    *ptrInt = 3;

    NSLog(@"%x,address:%x",*ptrInt,(uint)ptrInt);
//------------end------------

输出结果:

2014-08-17 23:26:37.376 LoopDemo[70897:303] 3,address:17be0

阵列与指标

阵列在宣告时使用连续的记忆体当阵列的空间,所以在使用上也可以利用指标的方式来存取,一般阵列宣告及将阵列印出来:

//-----------start-----------
    int arrayInt[4]={0,1,2,3};

    for (int i=0; i<4; i++) {
        NSLog(@"%i",arrayInt[i]);
    }

//------------end------------

输出结果:

2014-08-17 23:44:11.089 LoopDemo[71098:303] 0
2014-08-17 23:44:11.090 LoopDemo[71098:303] 1
2014-08-17 23:44:11.090 LoopDemo[71098:303] 2
2014-08-17 23:44:11.090 LoopDemo[71098:303] 3

使用指标的方式将阵列的内容印出之前,其宣告的指标变数的资料型态必需与阵列的资料型态相同:

//-----------start-----------
    int arrayInt[4]={0,1,2,3};
    int *ptrInt;
    ptrInt = &arrayInt[0];//取得阵列一开始的位址

    for (int i=0; i<4; i++) {
        NSLog(@"%i",*(ptrInt + i));
    }
//------------end------------

如此一来,在位址计算时所移动的指标才会符合阵列所宣告的资料型态,位址确认好后,使用*号取得其内容,像*(ptrInt + 1)所代表的是arrayInt[1]

输出结果:

2014-08-17 23:44:11.090 LoopDemo[71098:303] 0
2014-08-17 23:44:11.091 LoopDemo[71098:303] 1
2014-08-17 23:44:11.091 LoopDemo[71098:303] 2
2014-08-17 23:44:11.091 LoopDemo[71098:303] 3