What is the difference between
char cur_byte=*((char *)(buf+i));
and
char *b=(char *)(buf);
char cur_byte=*(b+i);
Assume:
buf is a pointer to void// void *buf; and
i is used as an iterator in a for loop
I found this code(the first line) in a c source code which generates rabin fingerprints and because VC2010 express reported it as an error I had to replace it with the second two lines. And I am not sure if it can do the intended purpose. Plus I would be grateful if anyone can give me a hint where to get a working C++ source code for content defined chunking and fingerprint generating.
In your first statement you add an integer (
iis an integer type, right?) to avoid*, casting tochar*afterwards. Pointer arithmetic with void pointers is not defined by the C standard, because the compiler has no way to know of how much it should increment the pointer. Some compilers, however, definesizeof(void) == 1. In this case, your two snippets are equivalent, which explains why this code may have worked with another compiler (thanks Steve Jessop for pointing this).What you meant in your first snippet was probably
char cur_byte=*(((char *) buf) + i);, the character pointed by the address locatedicharacters after buf.In the following schema, where
i==4,cur_bytewould be assigned the valuer.In your second statement:
you first assign
buftob, and then assign the content ofb + itocur_byte.bhas typechar*so addingiwill give the addressicharacters afterb.In the end these two statements are equivalent (except for the assignment of
b).