Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c obj-c中的参数按引用传递_Objective C_C - Fatal编程技术网

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>和 R2,你必须通过指针,或者使用C++引用。请注意,使用C++引用将要求您深入ObjtoVC++中,在这种情况下,您的文件命名为代码> > MM 而不是<代码> M>代码> < /P>
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)位于文件顶部。