Jake
Jake

Reputation: 16837

Advancing array pointer in C

I have a C array as follows:

 unsigned long arr[10];

On my machine, unsigned long is 8 bytes. I have a situation where I write 4 bytes using arr[0], and then need to find the address of the next byte in the array.

 void * ptr = arr[0] + (sizeof(unsigned long) / 2);

Will the above work ?

Upvotes: 1

Views: 984

Answers (1)

merlin2011
merlin2011

Reputation: 75545

No it will not. You should cast to char* first and then do the pointer arithmetic.

 void * ptr = ((char*) &arr[0]) + (sizeof(unsigned long) / 2);

Upvotes: 6

Related Questions