78.6k views
2 votes
Write the definition of a function named swapints that is passed two int reference parameters. The function returns nothing but exchanges the values of the two variables. So, if j and k have (respectively) the values 15 and 23, and the invocation swapints(j,k) is made, then upon return, the values of j and k will be 23 and 15 respectively.

User Iruleonu
by
6.2k points

1 Answer

2 votes

Answer:

void swapints(int *j,int *k)//Function definition.

{

*j=*j+*k-(*k=*j); //value swapping.

}

swapints(&j,&k);//call the function.

Step-by-step explanation:

  • The above function definition takes the address of j and k variables which is stored on the pointer variable j and k.
  • Then the pointer variable uses the j and k value for the above expression, and the user does not need to return the value of j and k.
  • But when the user prints the value of the j and k variable, then he gets the swapping value of the j and k variable.
  • The user needs to know that the "int j" is a normal variable, but "int *j" is a pointer variable that is used to take the address of j variable.
User Lalebarde
by
5.4k points