Re:Ask HN: Favorite pointer tricks in C?
by CountHackulus on Saturday November 20, 2010 @08:39PM
(#1925932)
My favorite pointer trick is a simple one. I learned it when I had to implement it (in 64-bit) in a C compiler.
Simply, you can subtract pointers. Let's say you're walking a string from the front and from the back at the same time, and want to find the length of the substring. Well, you don't have to use indeces, just do this:
int len = back_ptr - front_ptr;
You'd be surprised how often this crops up when you're using lots of pointer tricks.
Simply, you can subtract pointers. Let's say you're walking a string from the front and from the back at the same time, and want to find the length of the substring. Well, you don't have to use indeces, just do this:
You'd be surprised how often this crops up when you're using lots of pointer tricks.