指向指针的指针

- (void)test {

    NSString *name = @"111";
    NSLog(@"name:%@", name);
    
    BOOL success1 = [self changeValue1:name];
    if (!success1) {
        NSLog(@"name:%@", name);
    }
    
    BOOL success2 = [self changeValue2:&name];
    if (!success2) {
        NSLog(@"name:%@", name);
    }
    
    NSInteger age = 10;
    NSLog(@"name:%ld", age);
    
    BOOL success3 = [self changeAge1:age];
    if (!success3) {
        NSLog(@"name:%ld", age);
    }
    
    BOOL success4 = [self changeAge2:&age];
    if (!success4) {
        NSLog(@"name:%ld", age);
    }
    
    NSInteger *p_age = &age;
    BOOL success5 = [self changeAge3:&p_age];
    if (!success5) {
        NSLog(@"name:%ld", age);
    }
    
}

- (BOOL)changeValue1:(NSString *)name {
    
    name = @"222";
    return NO;
}

- (BOOL)changeValue2:(NSString **)name {
    
    *name = @"333";
    return NO;
}

- (BOOL)changeAge1:(NSInteger)age {
    age = 20;
    return NO;
}

- (BOOL)changeAge2:(NSInteger *)age {
    *age = 30;
    return NO;
}

- (BOOL)changeAge3:(NSInteger **)age {
    **age = 40;
    return NO;
}

打印结果为:

2021-08-05 15:25:20.451119+0800 OCTestLine[11790:311160] name:111
2021-08-05 15:25:20.451173+0800 OCTestLine[11790:311160] name:111
2021-08-05 15:25:20.451212+0800 OCTestLine[11790:311160] name:333
2021-08-05 15:25:20.451243+0800 OCTestLine[11790:311160] name:10
2021-08-05 15:25:20.451274+0800 OCTestLine[11790:311160] name:10
2021-08-05 15:25:20.451299+0800 OCTestLine[11790:311160] name:30
2021-08-05 15:25:20.451324+0800 OCTestLine[11790:311160] name:40
Program ended with exit code: 0

 

上一篇:二进制状态压缩对应 bool 数组中的常用操作


下一篇:No.272 - LeetCode[861] Score After Flipping Matrix - 贪心