Write a function which swaps two int* in c and also write call to that function [closed]
A nice C interview 开发者_如何学JAVAquestion:
Can you write a function which swaps two int* in C and also write a call to that function?
int a = 10, b = 20;
int* first_pointer = &a;
int* second_pointer = &b;
/* Below line should print (*first_pointer) = 10, (*second_pointer) = 20 */
printf("(*first_pointer) = %d, (*second_pointer) = %d\n",*first_pointer, *second_pointer);
/// **** Call your swap function here ****
/* Below line should print (*first_pointer) = 20, (*second_pointer) = 10 */
printf("(*first_pointer) = %d, (*second_pointer) = %d\n",*first_pointer, *second_pointer);
Function is here,
void swap(int** first_pointer, int **second_pointer)
{
int *temp = *first_pointer;
*first_pointer = *second_pointer;
*second_pointer = temp;
}
function call is here,
int a = 10, b = 20;
int* first_pointer = &a;
int* second_pointer = &b;
// Below will print (*first_pointer) = 10, (*second_pointer) = 20
printf("(*first_pointer) = %d, (*second_pointer) = %d\n",*first_pointer, *second_pointer);
swap(&first_pointer, &second_pointer);
// Below will print (*first_pointer) = 20, (*second_pointer) = 10
printf("(*first_pointer) = %d, (*second_pointer) = %d\n",*first_pointer, *second_pointer);
精彩评论