SoFunction
Updated on 2025-03-07

C++ pointer uses formal parameters to change actual parameters

Arrange 10 integers in order from small to large

#include <iostream>
using namespace std;
int main()
{
 //Use formal parameters to change the actual parameters //Order 10 integers from small to large void select_sort(int *p, int n);//Function declaration int a[10], i;
 cout << "enter the originl array:" << endl;
 for (i = 0; i < 10; i++) //Enter 10 numbers cin >> a[i];
 cout << endl;
 select_sort(a,10); //Function call, array name as actual parameter cout << "the sorted array:" << endl;
 for (i = 0; i < 10; i++) //Output 10 sorted numbers cout << a[i] << " ";
 cout << endl;
 system("pause");
 return 0;
}

void select_sort(int *p, int n) //Use pointer variables as formal parameters{
 int i, j, k, t;
 for (i = 0; i < n - 1; i++)
 {
 k = i;
 for (j = i + 1; j < n; j++)
  if (*(p + j) < *(p + k)) k = j; //Use pointer method to access array elements t = *(p + k); *(p + k) = *(p + i); *(p + i) = t;
 }

}

The above method of using formal parameters to change the actual parameters of the c++ pointer is all the content I share with you. I hope you can give you a reference and I hope you can support me more.