开发者

Write a function which swaps two int* in c and also write call to that function [closed]

开发者 https://www.devze.com 2023-01-22 20:51 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消