• 周日. 11月 27th, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

指向指针的指针

admin

11月 28, 2021

源代码:

- (void)test {

    NSString *name = @"111";
    NSLog(@">>>>>>>>>name:%@", name);
    
    [self changeValue1:name];
    NSLog(@">>>>>>>>>name:%@", name);
    
    [self changeValue2:&name];
    NSLog(@">>>>>>>>>name:%@", name);
    
    NSInteger age = 10;
    NSLog(@">>>>>>>>>age:%ld", age);
    
    [self changeAge1:age];
    NSLog(@">>>>>>>>>age:%ld", age);
    
    [self changeAge2:&age];
    NSLog(@">>>>>>>>>age:%ld", age);
    
    NSInteger *p_age = &age;
    [self changeAge3:&p_age];
    NSLog(@">>>>>>>>>age:%ld", age);
}

- (void)changeValue1:(NSString *)name {
    name = @"222";
}

- (void)changeValue2:(NSString **)name {
    *name = @"333";
}

- (void)changeAge1:(NSInteger)age {
    age = 20;
}

- (void)changeAge2:(NSInteger *)age {
    *age = 30;
}

- (void)changeAge3:(NSInteger **)age {
    **age = 40;
}

打印结果为:

2021-08-06 23:46:07.004362+0800 OCTestLine[15374:474778] >>>>>>>>>name:111
2021-08-06 23:46:07.004422+0800 OCTestLine[15374:474778] >>>>>>>>>name:111
2021-08-06 23:46:07.004487+0800 OCTestLine[15374:474778] >>>>>>>>>name:333
2021-08-06 23:46:07.004533+0800 OCTestLine[15374:474778] >>>>>>>>>age:10
2021-08-06 23:46:07.004569+0800 OCTestLine[15374:474778] >>>>>>>>>age:10
2021-08-06 23:46:07.004602+0800 OCTestLine[15374:474778] >>>>>>>>>age:30
2021-08-06 23:46:07.004633+0800 OCTestLine[15374:474778] >>>>>>>>>age:40
Program ended with exit code: 0

总结起来就是:

- (void)test {
    
    int a = 10;
    [self changeValue1:a];
    NSLog(@">>>>>>>>>>%d", a);
    
    [self changeValue2:&a];
    NSLog(@">>>>>>>>>>%d", a);
    
    int b = 100;
    [self exchangeValue1:a :b];
    NSLog(@">>>>>>>>>>%d,%d", a, b);
    
    [self exchangeValue2:&a :&b];
    NSLog(@">>>>>>>>>>%d,%d", a, b);
    
    [self exchangeValue3:&a :&b];
    NSLog(@">>>>>>>>>>%d,%d", a, b);
}

// int num;
// num = a;
// num = 20;
- (void)changeValue1:(int)num {
    num = 20;
}

// int *num;
// num = &a;
// *num = 30;
- (void)changeValue2:(int *)num {
    *num = 30;
}

// int num1 = a;
// int num2 = b;
// int num;
// num = num1;
// num1 = num2;
// num2 = num;
- (void)exchangeValue1:(int)num1 :(int)num2 {
    int num;
    num = num1;
    num1 = num2;
    num2 = num;
}

// int *num1 = &a;
// int *num2 = &b;
// int *num;
// num = num1;
// num1 = num2;
// num2 = num;
- (void)exchangeValue2:(int *)num1 :(int *)num2 {
    int *num;
    num = num1;
    num1 = num2;
    num2 = num;
}

// int *num1 = &a;
// int *num2 = &b;
// int num;
// num = *num1;
// *num1 = *num2;
// *num2 = *num;
- (void)exchangeValue3:(int *)num1 :(int *)num2 {
    int num;
    num = *num1;
    *num1 = *num2;
    *num2 = num;
}

打印结果为:

2021-08-06 23:42:26.266532+0800 OCTestLine[15333:472808] >>>>>>>>>>10
2021-08-06 23:42:26.266593+0800 OCTestLine[15333:472808] >>>>>>>>>>30
2021-08-06 23:42:26.266669+0800 OCTestLine[15333:472808] >>>>>>>>>>30,100
2021-08-06 23:42:26.266703+0800 OCTestLine[15333:472808] >>>>>>>>>>30,100
2021-08-06 23:42:26.266735+0800 OCTestLine[15333:472808] >>>>>>>>>>100,30
Program ended with exit code: 0

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注