Objective c obj-c中的参数按引用传递
我只是做了一项研究,但我有一个问题Objective c obj-c中的参数按引用传递,objective-c,c,Objective C,C,我只是做了一项研究,但我有一个问题 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { int r1=40,r2=5; swap(r1,r2); NSLog(@" temp is %d",r1); } void swap(int r1, int r2) { NSLog(@" m i 1st"); int
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
int r1=40,r2=5;
swap(r1,r2);
NSLog(@" temp is %d",r1);
}
void swap(int r1, int r2)
{
NSLog(@" m i 1st");
int temp;
temp=r1;
r1=r2;
r2=temp;
NSLog(@" temp is %d",r1);
}
我得到了冲突类型的
交换
;这样做正确吗?谢谢 > P>如果你想修改代码> R1 < /C>和void swap_with_pointers(int *r1, int *r2)
{
int temp;
temp = *r1;
*r1 = *r2;
*r2 = temp;
}
void swap_with_references(int &r1, int &r2)
{
int temp;
temp = r1;
r1 = r2
r2 = temp;
}
然后,使用以下实现之一:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
int x = 3;
int y = 4;
swap_with_pointer(&x, &y); // swap_with_references(x,y);
printf("x = %d, y = %d", x, y);
return 0;
}
输出,无论哪种方式:
x=4,y=3
很抱歉,它工作正常,刚刚得到警告这种方式在c99i中无效我得到了冲突类型的交换,这是做>>的正确方式吗?谢谢你是对的,但在obj-c中我得到警告,因为它的隐式交换声明在c99i中无效你只需要在使用前声明交换。添加行
void swap(int r1,int r2)代码>位于文件顶部。