111k views
5 votes
Below is the prototype for a function that takes two pointers to integer variables as its parameters. The purpose of the function is to exchange the values stored in the integer variables to which the two pointers point. Consider the proposed implementation code for this function and select all options that would correctly accomplish the intent of the function.

void exchange ( int p, int q );

A) void exchange( int p, int q ) {
p = q;
q = p;}
B) void exchange( int p, int q ) {
p = q;
q = p;}
C) void exchange( int p, int q ) {
int temp = *p;
p = q;
*q = temp;}
D) void exchange( int p, int q ) { int *temp = p; p = q; q = temp;}
E) void exchange( int p, int q ){ int temp = p; p = q; q = *temp;}

User Pholzm
by
3.6k points

1 Answer

2 votes

Answer:

The answer is "Option C".

Step-by-step explanation:

In the given method prototype a method "exchange" is declared, which accepts two integer parameters, that is "p and q", and this method can't return any value because its return type is void. In the method definition time is defined, that both variables is a pointer type, that's choices c is correct and others were wrong, that can be described as follows:

  • Option A and Option B both are wrong because it can't interchange the value.
  • Option D and Option E both option uses an integer pointer variable "temp", which is not defined in the question, that's why it is incorrect.
User Philayyy
by
4.4k points