我正在读一本书来学习Objective-C,这个程序应该展示处理指针的关键概念,我真的迷路了。
函数的参数中是否发生了某种转换,将p1
, p2
, &il
, 和指针&i2
的值 ( *
) 转换为指针的值 ( )?喜欢p1
变成*p1
?
我以为是变量的副本而不是实际变量被传递到函数中,那么为什么在函数之后传入的变量的值发生了变化?
另外,为什么我会在第 3 行收到一条警告,上面写着:No previous prototype for function 'exchangeValues'
?
谢谢!!
#import <Foundation/Foundation.h>
void exchangeValues (int *pint1, int *pint2) {
int temp;
temp = *pint1;
*pint1 = *pint2;
*pint2 = temp;
}
int main (int argc, char *argv[]) {
@autoreleasepool {
void exchangeValues (int *pint1, int *pint2);
int il = -5, i2 = 66, *p1 = &il, *p2 = &i2;
NSLog(@"il = %i, i2 = %i", il, i2);
exchangeValues(p1, p2);
NSLog(@"il = %i, i2 = %i", il, i2);
exchangeValues(&il, &i2);
NSLog(@"il = %i, i2 = %i", il, i2);
}
return 0;
}
输出:
2012-08-02 11:13:38.569 Test[381:707] il = -5, i2 = 66
2012-08-02 11:13:38.571 Test[381:707] il = 66, i2 = -5
2012-08-02 11:13:38.572 Test[381:707] il = -5, i2 = 66