What I think takes the cake is this: array[index] == index[array] Not that you would actually use this, but it gave me a lot of insight into how addressing and stuff works inside the compiler. Also from this example, there's the implicit suggestion that an array can be treated as a pointer. So that leads into pointer arithmetic which can be very useful.
Ask HN: Favorite pointer tricks in C?
51–60 of 81 posts
Re: Ask HN: Favorite pointer tricks in C?
#52Regarding function pointer arrays, I'm doing something in labrea ( http://github.com/dustin/labrea ) where I need to make what is effectively an FFI call from C to C where I know the number of arguments, and their types, but I have them in the form of an array of unions. For example, if I need to call read, it's basically an invocation of a function with a 4 byte, 8 byte, and then 4 byte argument (on a 64-bit system)…
It will certainly work for 90% of the common function types out there and is a pretty common trick.
Re: Ask HN: Favorite pointer tricks in C?
#53Re: Ask HN: Favorite pointer tricks in C?
#54Would you care to share the learning objectives you are fulfilling with pointer tricks in C? Or if it's just some extracurricular entertainment?
Re: Ask HN: Favorite pointer tricks in C?
#55Copy-free contiguous subsets of arrays are fairly simple but often convenient. If you want elements 5 through 33 of big_array, you just get a pointer to element 5, and keep track separately of the length. A common case is where you split an array into two non-overlapping subparts, in which case, if you no longer need the original, you can treat each subpart as if it were a separate array. Saves the work of allocating…
""
gets modified during parsing to become: ""
Your parse tree result result can then just contain pointers to "a\0", "href\0", and "blah\0" rather than doing any copying.Re: Ask HN: Favorite pointer tricks in C?
#56Re: Ask HN: Favorite pointer tricks in C?
#57* Using pointer offsets to get to the stack frame pointer, and then walking the frame pointer backwards to get the call stack. * Using && to take the address of a jump label. * Casting a u_int32_t over a 4-byte string (like an SMTP verb) to get a value you can switch() on.
Had to look that one up...turns out it's a GCC trick that allows you store the address of a jump label into a pointer to void. Later on, you can do "goto *ptr" to jump back to that address. Neat. See http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html
(You obviously already know this...just putting it here in case anyone else hasn't heard of it and is curious)
Re: Ask HN: Favorite pointer tricks in C?
#58If you have a struct/class with a lot of members that are usually set to zero or some other initial value, you can store them in a "lookaside" structure that is hung off a global hash table with the pointer of the original object as the hashtable key. You can then use a bitfield to keep track of which members actually have interesting data.
So -- accessing the member would look something like this:
int MyClass::get_foo() {
if (foo_set_)
return global_lookaside[this].foo;
return 0;
}Re: Ask HN: Favorite pointer tricks in C?
#59* Using pointer offsets to get to the stack frame pointer, and then walking the frame pointer backwards to get the call stack. * Using && to take the address of a jump label. * Casting a u_int32_t over a 4-byte string (like an SMTP verb) to get a value you can switch() on.
> Casting a u_int32_t over a 4-byte string (like an SMTP verb) to get a value you can switch() on. This is awesome beyond words. If I stumbled across this in the wild I would flip flop between awe and disgust until my head exploded. Let me guess, "SMTP verb" is not a hypothetical example?