What is the correct way to offset a pointer?

PICyourBrain picture PICyourBrain · Oct 6, 2010 · Viewed 44.5k times · Source

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

Answer

nos picture nos · Oct 6, 2010

That would do, but just make it:

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

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