Re:Ask HN: Favorite pointer tricks in C?
by _delirium on Saturday November 20, 2010 @05:45PM
(#1925591)
Copy-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 two new arrays for the split parts, which is necessary in many other languages. Useful for efficiently implementing things like decision-tree learners.
[ Parent
| on HN ]
Also useful for storing parse results:
"<a href='blah'>"
gets modified during parsing to become: "<a\0href\0'blah\0>"
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?
by deutronium on Sunday November 21, 2010 @06:24PM
(#1928193)
This is how strtok returns tokens too.
Incidentally I only found this after trying:
char tmp[] = "cat,mat,sat";
char *t;
t = strtok(tmp,",");
while(t != NULL){
printf("%s\n",t);
t = strtok(NULL,",");
}
And getting a segfault, as 'tmp' is not writeable memory.