1

A nice C interview question:

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);
SunnyShah
  • 28,934
  • 30
  • 90
  • 137
  • 1
    @Aistina: I think now you can see, I added question mark. :-) – SunnyShah Nov 02 '10 at 09:32
  • And what exactly is the point of this "question" ? You have answered your own question right. So ? Earning reputation? (: – Kiril Kirov Nov 02 '10 at 09:35
  • Adding a question mark to something doesn't automatically make that a question. If you had a proposed solution you should have added it into the question, not as an answer. Something like: here's what I came up with - is it good, what problems do you see? – sharptooth Nov 02 '10 at 09:36
  • @Kiril Kirov: Before taking interview I come to stackoverflow.com and see all the interview question, so I just added one more to library. I think there must be many like me. – SunnyShah Nov 02 '10 at 09:37
  • @Kiril Kirov: I require lots of reputation, I usually use it to give bounty. You can check my question list for that. :-) – SunnyShah Nov 02 '10 at 09:40
  • @Sunny - OK, right, sorry about that (: – Kiril Kirov Nov 02 '10 at 09:44
  • Possible answers are: yes, no and maybe. Mine is yes! – Luca Nov 02 '10 at 11:27
  • @Luca, @sharptooth, @Aistina: Tried to make it more real. – SunnyShah Nov 03 '10 at 07:10

1 Answers1

5

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);
SunnyShah
  • 28,934
  • 30
  • 90
  • 137
  • The *possible* problem I see here is that you manipulate uninitialized pointers - it is illegal in C++ and maybe in C as well - http://stackoverflow.com/questions/1866461/why-should-i-not-try-to-use-this-value-after-delete-this – sharptooth Nov 02 '10 at 09:52
  • Oh yes, your code contains undefined behavior - http://stackoverflow.com/questions/4076563/is-using-an-invalid-pointer-value-legal-in-c – sharptooth Nov 02 '10 at 10:03
  • I added a missing asterisk to the second_pointer declaration. – unwind Nov 02 '10 at 11:18
  • Corrected all the missing points. :-) – SunnyShah Nov 03 '10 at 07:11