carrayspointersoffset

What is the correct way to offset a pointer?


I want to pass a pointer to a function. I want this pointer to point to some place in the middle of an array. Say I have an array like such unsigned char BufferData[5000];, would the following statement be correct syntactically?

writeSECTOR( destAddress, (char *)( BufferData + (int)(i * 512 )) );
// destAddress is of type unsigned long
// writeSECTOR prototype: int writeSECTOR ( unsigned long a, char * p );
// i is an int

Solution

  • That would do, but just make it:

     writeSECTOR( destAddress, &BufferData[i * 512]);
    

    (It sounds like writeSECTOR really should take an unsigned char* though)