小编Aks*_*hat的帖子

在C++中将值传递给指针参数

在下面的代码中,为什么交换函数以两种方式工作,通过将值传递为swap(x,y)和swap(&x,&y).

int main(){

   int x, y;

   cout << "Enter the values of x and y: ";
   cin >> x >> y;

   swap(x, y);

   cout << "\nSwapped values are, x: " << x << " & y: " << y <<"\n";

   return 0;

}

void swap(int *a, int *b){

   int s;
   s = *a;
   *a = *b;
   *b = s;

}
Run Code Online (Sandbox Code Playgroud)

c++ pointers parameter-passing

4
推荐指数
2
解决办法
130
查看次数

标签 统计

c++ ×1

parameter-passing ×1

pointers ×1