06 Jo P June 07


Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

06 Jo P June 07

  1. 1. www.linuxforu.com | LINUX FOR YOU | JUNE 2007 C M Y K 87 I n my previous column, we saw some of the pitfalls of using + and – operators to swap two variables without using a temporary variable: *i = *i + *j; *j = *i - *j; *i = *i - *j; Think about the logic in using + and – for such swapping. Can we use any other operators for a similar purpose? Yes, * and / are complementary operators and they can be used for such swapping as well, following the same logic: *i = *i * *j; *j = *i / *j; *i = *i / *j; It works. However, this solution has two problems: if i and j are big, then *i * *j will overflow and by using the division operation you can’t get the original values back; and if *j is 0, the *i / *j expression will result in a divide by zero exception. What about the two complementary bit-wise operators & and | ...can we use them for swapping by following similar logic? *i = *i & *j; *j = *i | *j; *i = *i | *j; No, it doesn’t work. Unlike + and *, the & operator is not really an operator that retains (or accumulates) both the values in some form that using | twice could retrieve. Try it once to confirm it yourself. However, the bitwise ^ can fit the requirement (though it doesn’t have a complementary operator like + and –, * and / operators): *i = *i ^ *j; *j = *i ^ *j; *i = *i ^ *j; It works because the ^ operator complements itself (if either of the bits is set, the result is true, and if both bits are same, the result is false)...it’s indeed a curious operator! Bitwise operations are only for integral values; so this swapping trick cannot be used for floating-point numbers. None of the tricks we discussed will work well for swapping two pointers. Other than binary -, no other binary arithmetic operators (+, *, /, %) can be used for two pointers. A pointer represents the address and subtracting two addresses is acceptable (to see the offset or difference between the two locations, for example, in an array), and using other arithmetic operators with two pointers will result in compiler error. Also, it is incorrect to apply ^ on two pointers. You can cast the operands to integers and apply ^, but it’s a bad approach for many reasons. For example, the conversion would fail if the pointer and integer sizes are different. For practical uses, it is best to stick to the plain and straight- forward approach of using a temporary variable for swapping. Find the bug Here is a swap function for swapping two strings: void swapStr(char *i, char *j){ char *temp = i; i = j; j = temp; } Looks fine and comfortable, doesn’t it? But it does not work: char *i = “first”; char *j = “second”; printf(“Before swap : %s %sn”, i, j); swapStr(i, j); printf(“After swap : %s %sn”, i, j); // output: // Before swap : first second // After swap : first second Can you find what went wrong? S.G. GANESH Let us look at the interesting problem of swapping two variables without using any temporary variables. We will discover the possible pitfalls of using smart ‘tricks’ to solve the problem. This column is to help students and novices understand C better. The Joy of Programming C Traps and Pitfalls: Swapping Two Variables–Part II S G GaneshS G GaneshS G GaneshS G GaneshS G Ganesh is an engineer in Hewlett-Packard’s C++ compiler team. He is a member of the ANSI/ISO C++ Standardisation committee (JTC1/SC22/WG21). You can reach him at sgganesh@gmail.com. GUESTCOLUMN