The canonical evil pointer trick is using prev_xor_next to construct a doubly linked list.
Could you put an example?
Ask HN: Favorite pointer tricks in C?
11–20 of 81 posts
Re: Ask HN: Favorite pointer tricks in C?
#12You can demonstrate pointer arithmetic by showing how you would work with the strstr function. It's the clearest and most understandable reason for someone to see why you'd even discuss this topic I think. I talk to some people without C experience and they hear that idea and get scared. I usually explain how strstr works and that seems to always make sense to them. Good luck!
I actually write this a lot in my code. Not code that I intend to share with others, of course. But I do find it amusing about the value I put in brackets. I find it amusing that I have an OCD-like predisposition to make it a power of two. And I find it amusing how the number between the brackets has increased over the last ten years, from a frugal 64 to an opulent 1024. This, to me, is progress.
Re: Ask HN: Favorite pointer tricks in C?
#13For e.g., a constraint in an AVL tree requires that the difference in sizes of left and right subtrees be -1, 0 or 1 (just 3 values, which requires 2 bits). A 4-byte aligned pointer would be enough. =)
Re: Ask HN: Favorite pointer tricks in C?
#14 int moved = (is_reading ? read : write)(fd, buf, len);Re: Ask HN: Favorite pointer tricks in C?
#15You can demonstrate pointer arithmetic by showing how you would work with the strstr function. It's the clearest and most understandable reason for someone to see why you'd even discuss this topic I think. I talk to some people without C experience and they hear that idea and get scared. I usually explain how strstr works and that seems to always make sense to them. Good luck!
char buf[1024]; /* should be enough */ I actually write this a lot in my code. Not code that I intend to share with others, of course. But I do find it amusing about the value I put in brackets. I find it amusing that I have an OCD-like predisposition to make it a power of two. And I find it amusing how the number between the brackets has increased over the last ten years, from a frugal 64 to an opulent 1024. This, t…
Also, making it a power of 2 is a good idea if you are concerned about alignment.
Re: Ask HN: Favorite pointer tricks in C?
#16struct packet_header { uint from_addr; uint to_addr; ushort flags; ... }
packet *p;
read(socket, somebuf, sizeof(packet));
p = &somebuf;
printf("from = %u to = %u flags = %u\n",p->from_addr, p->to_addr, p->flags);
Re: Ask HN: Favorite pointer tricks in C?
#17Re: Ask HN: Favorite pointer tricks in C?
#18Earlier quoted context omitted.
char buf[1024]; /* should be enough */ I actually write this a lot in my code. Not code that I intend to share with others, of course. But I do find it amusing about the value I put in brackets. I find it amusing that I have an OCD-like predisposition to make it a power of two. And I find it amusing how the number between the brackets has increased over the last ten years, from a frugal 64 to an opulent 1024. This, t…
In fact, a lot of code does this to avoid mallocs. Just be sure to check the length before you put anything into the buffer. Also, making it a power of 2 is a good idea if you are concerned about alignment.
No need to check. The comment reassures both me and future maintainers that I have countenanced this potential pitfall! Assign away....