Objective-C 指针算术

  • 指针算术

    Objective-C指针是一个地址,它是一个数值。因此,您可以像对数值一样对指针执行算术运算。可以在指针上使用四种算术运算符:++,-,+和-为了理解指针算术,让我们考虑ptr是一个指向地址1000的整数指针。假设32位整数(4字节),让我们对指针执行以下算术运算-
    
    ptr++
    
    现在,在执行上述操作之后,由于ptr每次递增,因此ptr将指向位置1004,它将指向下一个整数位置,即当前位置旁边的4个字节。该操作会将指针移动到下一个存储位置,而不会影响该存储位置的实际值。如果ptr指向地址为1000的字符,则上述操作将指向位置1004,因为下一个字符在1004处可用。
  • 递增指针

    我们更喜欢在程序中使用指针而不是数组,因为变量指针可以增加,而不像数组名是常量指针那样不能增加。以下程序递增变量指针以访问数组的每个后续元素-
    
    #import <Foundation/Foundation.h>
    
    const int MAX = 3;
    
    int main () {
       int  var[] = {10, 100, 200};
       int  i, *ptr;
    
       /* let us have array address in pointer */
       ptr = var;
       for ( i = 0; i < MAX; i++) {
          NSLog(@"Address of var[%d] = %x\n", i, ptr );
          NSLog(@"Value of var[%d] = %d\n", i, *ptr );
    
          /* move to the next location */
          ptr++;
       }
       return 0;
    }
    
    当上面的代码被编译和执行时,产生的结果如下:
    
    2020-08-08 09:21:22.303 test[13484:5112] Address of var[0] = 61fed8
    2020-08-08 09:21:22.319 test[13484:5112] Value of var[0] = 10
    2020-08-08 09:21:22.319 test[13484:5112] Address of var[1] = 61fedc
    2020-08-08 09:21:22.319 test[13484:5112] Value of var[1] = 100
    2020-08-08 09:21:22.319 test[13484:5112] Address of var[2] = 61fee0
    2020-08-08 09:21:22.319 test[13484:5112] Value of var[2] = 200
    
  • 递减指针

    相同的注意事项适用于递减指针,指针的值按其数据类型的字节数减少,如下所示-
    
    #import <Foundation/Foundation.h>
    
    const int MAX = 3;
    
    int main () {
       int  var[] = {10, 100, 200};
       int  i, *ptr;
    
       /* let us have array address in pointer */
       ptr = &var[MAX-1];
       for ( i = MAX; i > 0; i--) {
          NSLog(@"Address of var[%d] = %x\n", i, ptr );
          NSLog(@"Value of var[%d] = %d\n", i, *ptr );
    
          /* move to the previous location */
          ptr--;
       }
       return 0;
    }
    
    当上面的代码被编译和执行时,产生的结果如下:
    
    2020-08-08 09:25:25.990 test[11520:13412] Address of var[3] = 61fee0
    2020-08-08 09:25:26.000 test[11520:13412] Value of var[3] = 200
    2020-08-08 09:25:26.000 test[11520:13412] Address of var[2] = 61fedc
    2020-08-08 09:25:26.000 test[11520:13412] Value of var[2] = 100
    2020-08-08 09:25:26.000 test[11520:13412] Address of var[1] = 61fed8
    2020-08-08 09:25:26.000 test[11520:13412] Value of var[1] = 10
    
  • 指针比较

    可以通过使用关系运算符(例如==,< 和 >)来比较指针。如果p1和p2指向彼此相关的变量(例如同一数组的元素),则可以有意义地比较p1和p2。以下程序通过增加变量指针来修改前面的示例,只要它指向的地址小于或等于数组的最后一个元素的地址,即&var[MAX-1]-
    
    #import <Foundation/Foundation.h>
    
    const int MAX = 3;
    
    int main () {
       int  var[] = {10, 100, 200};
       int  i, *ptr;
    
       /* let us have address of the first element in pointer */
       ptr = var;
       i = 0;
       
       while ( ptr <= &var[MAX - 1] ) {
          NSLog(@"Address of var[%d] = %x\n", i, ptr );
          NSLog(@"Value of var[%d] = %d\n", i, *ptr );
    
          /* point to the previous location */
          ptr++;
          i++;
       }
       return 0;
    }
    
    当上面的代码被编译和执行时,产生的结果如下:
    
    2020-08-08 09:31:25.255 test[2184:2716] Address of var[0] = 61fed8
    2020-08-08 09:31:25.261 test[2184:2716] Value of var[0] = 10
    2020-08-08 09:31:25.261 test[2184:2716] Address of var[1] = 61fedc
    2020-08-08 09:31:25.261 test[2184:2716] Value of var[1] = 100
    2020-08-08 09:31:25.261 test[2184:2716] Address of var[2] = 61fee0
    2020-08-08 09:31:25.262 test[2184:2716] Value of var[2] = 200