Void * vs. char * pointer arithmetic

I am looking through my tutorial, and I'm a little confused by some of the code that is there. In one part, they perform pointer arithmetic as follows:

void* bp; ... bp = (void*)((char*)(bp)+16); ... 

but later they do the following:

 void* bp; ... bp = bp+16; ... 

I feel that they should be two different things, but they see it as one and the same. I feel that way because, for example, if you want to make access to an array (for example, for an integer array), you should do the following

 int* a = malloc(n*sizeof(int)); ... q = *(a+1); ... 

In this case, I do not get access to the next 4 bytes in the integer array, and not in the next byte? Similarly, I feel that if I have void * a, then * (a + 1) should be the next 4 bytes ... Or is it not? Thanks.

+7
source share
1 answer

This is a slippage. Arithmetic on void * not defined by the standard, but some compilers offer it as an extension, just like char * for arithmetic. The second is formally invalid C, but apparently slips out of a (bad) habit.

+11
source

All Articles