My C pointers were (and still are) slightly rusty. So I spend the evening brusing up on pointers in C. So I started with this super simple example: reversing a string.
This is a pretty simple task, one that only took me a few minutes, but illustrates C pointer usage nicely. This code snippet takes a string, allocates memory for the new string, iterates from the end of the string back to the beginning and formulates the new string by incrementing a pointer thru the memory allocated for the resulting (reversed) string.
This is pretty damn efficient and only uses the
strlen() function from the C string.h library.
char * string_reverse(char *);
char *string = "desi\0";
char * result = string_reverse(string);
char * string_reverse(char *string)
char *result = (char *)malloc(strlen(string));
char *j = result;
for(i = string+strlen(string)-1; i >= string ;i--)
*j = *i;