美文网首页
函数值传递、地址传递(引用)

函数值传递、地址传递(引用)

作者: json_jie | 来源:发表于2015-04-07 11:27 被阅读68次

int exchangeValue(int *a,int *b);

  • (void)viewDidLoad {
    [super viewDidLoad];

pragma 值传递

int k = 100;
NSLog(@"1   %d      %p",k,&k);
[self inputValue:k];
NSLog(@"3   %d      %p",k,&k);
/**
 *      1   100      0x7fff524b7abc
        2   110      0x7fff524b7a5c
        3   100      0x7fff524b7abc
 */

pragma mark 值传递

NSString *s = @"ABC";
NSLog(@"1   %@ %p",s,&s);
[self sInputValue:s];
NSLog(@"3   %@ %p",s,&s);

/**
 *       1   ABC 0x7fff52c78ab0
         2   BDC 0x7fff52c78a58
         3   ABC 0x7fff52c78ab0
 */

pragma mark 地址传递

NSLog(@"1   %@ %p",s,&s);
[self sInputExchangeValue:&s];
NSLog(@"3   %@ %p",s,&s);
/**
 *       1   ABC 0x7fff591feab0
         2   LLLLLLLL 0x7fff591feaa8                              // 地址传递,临时地址
         3   LLLLLLLL 0x7fff591feab0
 */

pragma mark 地址传递

int a = 100;
int b = 99;
NSLog(@"1   %d     %p    %d    %p" ,a,&a,b,&b);
exchangeValue(&a, &b);
NSLog(@"3   %d     %p    %d    %p" ,a,&a,b,&b);

/**
        1   100     0x7fff5e285aa4    99    0x7fff5e285aa0
        2   99      0x7fff5e285a38    100    0x7fff5e285a30       // 地址传递,临时地址
        3   99      0x7fff5e285aa4    100    0x7fff5e285aa0
 */

pragma mark ----- 地址传递 (对照前后对比地址对比准确的说是 函数指针的拷贝)

// (1)
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4", nil];
NSLog(@"1 %p",&array);
[self exchangeArrayValue:array];
NSLog(@"3 %p",&array);

for (int i = 0; i < array.count; i++) {
    NSLog(@"%@",[array objectAtIndex:i]);
}
/*
     1 0x7fff5f82cab0
     2 0x7fff5f82ca18
     3 0x7fff5f82cab0
     1
     2
     22
     4
 */

// (2)
// NSArray pointArray = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4", nil];
// NSLog(@"1 point %p",&pointArray);
// [self exchangePointArrayValue:pointArray];
// NSLog(@"4 point %p",&pointArray);
//
// for (int i = 0; i < pointArray.count; i++) {
// NSLog(@"%@",[pointArray objectAtIndex:i]);
// }
// /

// 1 point 0x7fff5a963ab0
// 2 point 0x7fff5a9639f8
// inpoint 1
// inpoint 2
// inpoint 3
// inpoint 4
// 3 point 0x7fff5a9639f8
// in2 point 2
// in2 point 3
// in2 point rrr
// 4 point 0x7fff5a963ab0
// 1
// 2
// 3
// 4
// */

}

-(void)exchangeArrayValue:(NSMutableArray *)ary
{

NSLog(@"2 %p",&ary);

// NSMutableArray *muArray = [[NSMutableArray alloc] initWithObjects:@"ppp",@"ooo",@"lll",@"kkkk", nil];
// ary = muArray;
[ary replaceObjectAtIndex:2 withObject:@"22"];

NSMutableArray *muArray = [[NSMutableArray alloc] initWithObjects:@"ppp",@"ooo",@"lll",@"kkkk", nil];
ary = muArray;

}
-(void)exchangePointArrayValue:(NSArray *)ay
{
NSLog(@"2 point %p",&ay);
for (int i = 0; i < ay.count; i++) {
NSLog(@"inpoint %@",[ay objectAtIndex:i]);
}
NSArray *a = @[@"2",@"3",@"rrr"];
ay = a;
NSLog(@"3 point %p",&ay);
for (int i = 0; i < ay.count; i++) {
NSLog(@"in2 point %@",[ay objectAtIndex:i]);
}
}

-(int)inputValue:(int)k
{
k = k+10;
NSLog(@"2 %d %p",k,&k);
return k;
}

-(void )sInputExchangeValue:(NSString **)p
{
p = @"LLLLLLLL";
NSLog(@"2 %@ %p",
p,p);

}
int exchangeValue(int *a,int *b){
int m = *b;
*b = a;
a = m;
NSLog(@"2 %d %p %d %p" ,
a,&a,
b,&b);

return 100;

}
-(NSString *)sInputValue:(NSString *)p
{
p = @"BDC";
NSLog(@"2 %@ %p",p,&p);
return p;
}

相关文章

网友评论

      本文标题:函数值传递、地址传递(引用)

      本文链接:https://www.haomeiwen.com/subject/gdjzxttx.html