programming | html tutorial | articles

up ]

 

 C Tips and Tricks


To swap between 2 variable without using third variable

Use this code to swap values between variable a and variable b. This method is much faster than using 3rd variable. assuming that a and b is integer.

a^=b;
b^=a;
a^=b;

And to add more fun, A and B variables can be in any format, from pointer to long floating point format. It uses XOR method to XORed the bits. Try it for yourself if you are not sure what I'm talking about.


To detect a null pointer error in TurboC 2 and Turbo C++ 3
Null Pointer error occur when a pointer which were pointed to a null address location where used in writing or reading. Therefore when the program exits, it will say null pointer error. It is very hard to detect this type of error using TC 2.0 or TC++ 3. But there are ways to overcome this problem.

To detect this type of error you have to use try and error method. Place a exit() statement in your suspected line. If the program exit at that line and there is no indication that the error had occur, then move your exit() command further to the next subsequent line and rerun your program. If there is error indicated therefore the error must be on the previous line before the exit() statement. Check throughly on that section.

 

c programming | html tutorial | articles | home
about me | links | search | guestbook | photo album